0

I understand why this doesn't work as the subclass isn't a forward reference, but is something along these lines with the same theoretical functionality possible?.

This is the implementation I'd use if I could forward reference the subclass array:

class_declare.h:

class parentClass{
   int i;
}childClass[100];

someSource0.cpp:

include "class_declare.h"

//manipulate values of the array - but can this refer to one that is forward declared
//and not a new object?
void dosomething(){
   childclass[1]=10;
}

someSource1.cpp:

include "class_declare.h"

void dosomethingelse(){
   std::cout<<childclass[1];
}

Output:

10

Thank you!.

s33ds
  • 57
  • 7
  • 1
    There is no subclass here. There's an array of 100 `parentClass`es and it's called "childClass". Perhaps you need [a good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? – molbdnilo Oct 03 '14 at 14:45
  • not *forward declare*, but just *declare*: `extern parentClass childclass[100];` in header and `parentClass childclass[100];` in some cpp file – Piotr Skotnicki Oct 03 '14 at 14:54
  • I understand that I've created an array of the class type, regardless of the mislabeling of the array, I'm wondering if this is possible. – s33ds Oct 03 '14 at 14:54
  • Thank you Piotr S!. Quick and easy :D. What can I give you in return?. – s33ds Oct 03 '14 at 15:20

1 Answers1

1

polymorphism should allow you to create a list of parent objects and populate it with children.

class ParentClass{
    int i;
};
ParentClass* ChildClass[100];

this declares a list which can contain 100 instances of ParentClass or any of its inheretors.

http://www.cplusplus.com/doc/tutorial/polymorphism/

ragingSloth
  • 1,094
  • 8
  • 22