-2

In header:

class foo : public ISceneNode
{
public:
   foo(){}

   void setAge(int a) { age = a; }

   int getAge(){ return age; }

private:
   int age;
}

In source:

foo::foo()
:ISceneNode(0,0)
{
    age = 5;
}

For example, I have this class with a member variable. The member variable is 5. In main:

foo* node = (foo*)smgr->addCubeSceneNode(2);
int age = node->getAge();

It works, however the problem is, program is now unaware of the member variable, since I have not created foo() with new. So, getAge() method returns nothing!

My question is, how do I initialize the member variable?

Thank you.

Cobra
  • 63
  • 1
  • 7
  • What does `smgr->addCubeSceneNode(2)` do, and why do you treat the result as a `foo*`, when it doesn't point to a real `foo`? – Beta May 25 '14 at 03:17
  • 3
    The question makes no sense to me. – user3344003 May 25 '14 at 03:17
  • 1
    This is a classic XY problem. You're asking how to do something which probably should never be done. Please back up and ask a new question about whether `(foo*)smgr->addCubeSceneNode(2);` is the right thing to do, providing its context. – Potatoswatter May 25 '14 at 03:18
  • I would second @Potatoswatter - please post some surrounding code and the actual error/issue you are getting. Are you simply doing a memory allocation (`malloc`) without a `new`? I would prefer not to second-guess your problem. :) – metacubed May 25 '14 at 03:26
  • cubeSceneNode is an another derived class, which is IMeshSceneNode, of ISceneNode. – Cobra May 25 '14 at 03:29
  • That's an explanation that explains nothing. – Beta May 25 '14 at 03:30
  • You need to study a book. There are free books on the net. – Cheers and hth. - Alf May 25 '14 at 03:46

2 Answers2

0

Your variable age is not marked public. This is the reason you cannot access it from outside the class. Creating an object with new has nothing to do with your problem.

class foo : public ISceneNode
{
   foo(){}
public:
   int age;
};

Unrelated note: You might want to make foo() public too.

metacubed
  • 7,031
  • 6
  • 36
  • 65
  • Or use `struct` instead of class… just a good default rule for interfaces where access hasn't yet been thought-out. However, there seem to be deeper issues here. – Potatoswatter May 25 '14 at 03:19
  • Sorry, actually those are public, I am typing from phone, thats why getting a little bit confused. I have a public method, getAge(). – Cobra May 25 '14 at 03:21
  • @Potatoswatter Agree about the code smell. Something's not quite right here. – metacubed May 25 '14 at 03:23
  • Personally I think the problem is the missing semicolon, since the code that doesn't compile can't possibly do what it's intended to do. – Cheers and hth. - Alf May 25 '14 at 03:45
0

frequently member variables of class are declared as private variables, because we can prevent unprotected accesses.

class foo: public ISceneNode
{
    foo(){}
    int age;
};

This snipet creates a class named foo(usually, we capitalize class name, such as Foo) which derives from ISceneNode. It is the relationship of "is-a".

keyword class indicates the default access rule is private while keyword strcut means public.

non-static Member variables are initialized through constructors. As stated before, your constructor is private, you can not access it through an object.

foo obj; // not allowed

if you add "public:"

class foo: public ISceneNode
{
int age;
public:
    foo(){age = 5;}
};

foo obj; // ok, age now equals 5;

But you also have to deal with the initialization of base class. Above code require the base class has a default constructor.

chunyang.wen
  • 204
  • 1
  • 9
  • Yes, the base class has default construction and that class is a part of the irrlicht library. I need to re-edit my post a little. – Cobra May 25 '14 at 03:46