NationStates Jolt Archive


Any C++ coders here?

Ginnoria
02-01-2006, 05:08
Not really sure how much of a programming community NS General has ... but I'm not getting much luck in my other forums. So here it is.

I have a base class (call it A) from which I have several derived classes. A has a CArray<A*> data member (dynamic array of pointers to A objects). When adding an object of B (class derived from A), none of B's data members show up when the object is accessed from the CArray of the first A object. Apparently, the B object is not being copied somehow. I'm currently using this code:


class A
{
A() {}

void AddChild(A* child)
{
m_aryChildren.Add(child);
}

CArray<A*> m_aryChildren;
}

class B : public A
{
B()
{
A::AddChild(new A());
}
}

// ....

A aobject();
aobject.AddChild(new B(5, 7));
int size = aobject[0].m_aryChildren.GetSize();


the 'size' variable should be 1. Yet it's showing up as zero, as if the B object I passed never had any of its data members set. Does anyone know what the problem is?
Worlorn
02-01-2006, 05:48
It's been a while since I did anything with c++, but I might be able to help you if I could actually see your code, which isn't showing up on my browser. Also, you should try your luck over at the userfriendly forums, they know their stuff.
Ginnoria
02-01-2006, 05:55
Here it is -codetags. And thank you, I will.

class A
{
A() {}

void AddChild(A* child)
{
m_aryChildren.Add(child);
}

CArray<A*> m_aryChildren;
}

class B : public A
{
B()
{
A::AddChild(new A());
}
}

// ....

A aobject;
aobject.AddChild(new B());
int size = aobject[0].m_aryChildren.GetSize();
Divine Imaginary Fluff
02-01-2006, 09:41
m_aryChildren uses A pointers, and thus only the inherited A properties of the B objects can be accessed from it. (to explain further: a pointer to class X is only aware of X's properties, not of the properties of inherited classes) You can solve this by casting a pointer in m_aryChildren to the appropriate type (B*, if you need to access something B-specific, for example) when using it. (or you can make a temporary B pointer and assign it the address of an A pointer pointing to a B object and then use the B pointer)

Also, in order to use functions from inherited classes using pointers to the base class directly, make them virtual and add virtual functions of the same name to the base class as well. (they can be empty shells if you are not going to use them for A objects. or even better pure virtual if you are not going to use A objects directly at all)
Divine Imaginary Fluff
02-01-2006, 10:14
Also, something that seems a little odd to me: When a new B object is created, it in turn automatically creates a new A object and adds it to its own inherited m_aryChildren array, so every B object will start out with one A child in it's array. Just so you know, in case this isn't how you want it. (I wonder as you didn't describe any need for this when presenting your example, and it at a glance looks more like some kind of error than a typical feature)