C++ Classes and Arrays

Is the statement below legal. Can I implement an array like this when working with a derived class Memorandum. If so how? If not Why?

 
  Memorandum memoArray[10];
It should be legal.
Make sure to have the base class' destructor virtual, if Memorandum stores more data than its base class.

These examples will be correct, showing you what I mean.
1
2
3
4
5
6
7
8
9
struct myBase {
    int data;
};
struct myDerived : public myBase {
    int get() { return data; }
};

//...
myDerived d[10];


1
2
3
4
5
6
7
8
9
10
struct myBase {
    inline virtual ~myBase() {}
};
struct myDerived : public myBase {
    int data;
    int get() { return data; }
};

//...
myDerived d[10];


I hope you see the differences between the examples.
Last edited on
It is legal if Memorandum has a default constructor (or is a POD)
Ok thank you EssGeEich. Am I not giving enough information about my other piece of the program or is it normally legal for all casses?
--Thanks once again!
I think it should be ok for all cases.
But you won't be able to use Memorandum as a class deriving from Memorandum, keep this in mind ( If you do it, it's a compilation error, so don't worry about runtime errors ).
Ok is that 'ambiguity'? My base class is 'Document' so no worries with regards to that.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class	Document
{
	private:
		string dNumber;

	public:
		Document(string dN);
		string getDocumentNumber() const;
		void virtual print() const;

};

class Memorandum : public Document
{
	private:
		string memorandumText;
	public:
		Memorandum(string dNum, string memText);
		void setText(string memTexts);
		string getText() const;
		void print();
};


--Thanks alot have a good day!
Last edited on
As coder77 has said, all that matters is the default constructor. Of course, the class also has to be concrete (i.e. no pure virtual methods.). So if Memorandum is the most derived class, it doesn't matter how deep the hierachy is.

A virtual destructor is only required if you plan to delete though a base class pointer, which doesn't figure here as you're using an array of the actual (derived) class. That is,

1
2
3
4
5
6
7
8
9
10
class Base { /* etc */ };
class Memorandum : public Base { /* etc */ };

Base* basePtrArray[10] = {0};

basePtrArray[0] = new Memorandum;

// etc

delete basePtrArray[0]; // this requires a virtual destructor 


(But it is, of course, good practice to provide a virtual destructor when working with a class with virtual functions; so you don't forget to do it when it is required.)

But what do you mean by working with a derived class?

Do you plan to do stuff like

1
2
3
4
5
6
7
Memorandum memoArray[10];

Base base;
// etc

memoArray[0] = base;
// etc 


Or

1
2
3
4
5
6
7
8
9
10
11
void foo(Base& base);

//etc

Memorandum memoArray[10];

// etc

foo(memoArray[0]);

// etc 


Andy
Last edited on
Your class is ALMOST perfect.
The only thing which is missing is the virtual destructor.
1
2
3
4
5
6
7
8
9
10
11
class	Document
{
	private:
		string dNumber;

	public:
		Document(string dN);
		string getDocumentNumber() const;
		void virtual print() const;
                inline virtual ~Document() {}
};

As it's inline, you don't need to add it to the Source file, it can simply stand in the header one.

There's no sign of overloading in your classes, in fact there's nothing to overload.

That's a simple derivation of a virtual class, overloading is a different concept.

EDIT:
All right, in fact as andywestken said (and coder777), you cannot call a constructor when you're initializing an array of data.
You need a default constructor for Memorandum.

@andywestken: He's using the Virtual keyword, there should be no problems with putting a virtual destructor in there.
Last edited on
@EssGeEich AH ok! I just left it to use the default destructor. Also the example helps a lot! I understand.

@Andy Yes to that second piece of code, I think what they are asking is if it is right to do it like that?

1
2
3
4
5
6
Memorandum memoArray[10];

Base base;
// etc

memoArray[0] = base;


and if not why not?
Try overloading the constructor: Make one the default constructor and have the other get the parameters you want.

Example:

1
2
3
4
5
6
7
8
9
10
11
class Memorandum : public Document
{
	private:
		string memorandumText;
	public:
		Memorandum(string dNum, string memText);
		Memorandum();
		void setText(string memTexts);
		string getText() const;
		void print();
};


And then:
1
2
Memorandum memoArray[10];
memoArray[0] = Memorandum("mytext1","mytext2");
Ok I see I got it thank you once again to all. YOu guys really reply fast on this site!!
It's very time-of-day dependent I think.
Topic archived. No new replies allowed.