PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Any C++ Guru's? :)

I have a small question:
I have these classes:

Code:

class A
{
A(x,y,z);
~A;
};

class B : public A
{
dosomethingcool();
};


Now I want to instantiate class B, and I do it like this:

Code:

B instance_of_B = new B(1,2,3);


This doesn't work. Why not? Shouldn't class B automatically use the ctor of A when it's not available in the same form in class B, just like any other method?

Right now, I fixed it like this:

Code:

class B : public A
{
B(x,y,z) { A::A(x,y,z); }
dosomethingcool();
};


This doesn't work either; the compiler complains about a missing default ctor. So I added one to class A:

Code:

class A
{
A();
A(x,y,z);
~A;
};


Now it compiles. But does this work like I expect? I mean, will the A ctor (the one with the three parameters) be called when I instantiate B using three parameters? And why do I need the default ctor?

I would really like to understand this mess. :)

- Jacco.
Last modification: Phantom - 01/03/02 at 11:35:02
Wow, hard to believe that no one here has responded to your C++ question.  It's actually funny because by the number of replies to other programming questions, there are more people here who know ARM assembly language and how to optimize things like alpha blitting code than simple C++ class design.

Anyway, here's the solution to your problem.

class A
{
public:
     A (int x, int y, int z){}
     ~A (){}
};

class B : public A
{
public:
     B (int x, int y, int z) : A (x, y, z){}
     ~B (){}
     void do_something_cool (){}
};


I'm assuming that your code actually declares your ctors and dtors as public and you mistakenly left this out in your post?

You need to add an initializer list in B's ctor so that when B's ctor is called, it will call the appropriate ctor of A, using the three variables passed to B's ctor.

Without the intializer list, the compiler is going to generate code to call A's default ctor, which you didn't implement.  When you did implement it, the compiler called this default ctor, then executed the code you had in B's ctor (which called A's ctor with the 3 parameters).  In this case the end result is the same as using the initializer list except you have the overhead from the call to the default A ctor.

Digby:somehow it's hard to believe that Jacco who is one of the best coders around here wasn't able to solve this "simple c++ class design"

Well, I don't do any C++ stuff , pure C is enought for me and I don't really see a point in making all this stuff. Any asm.. hey, asm is simple math and nothing more:)
Thanks a lot for the clarifications... It's very clear now.

And yes, I'm that bad at C++.... My 'coding skills' are really in the algorithm area, I'm not a specialist in any programming language. C++ is however the language that I understand best, it's a perfect tool to model thoughts while retaining control of low-level stuff.
amazing ! this question was answered within 3 hours! :)

just a little to share: all constructors and destructors must be declared public. the logic is, how would you construct the object if constructor is unaccessible? hehee...
Maybe you want a class that can only be constructed by friend classes or derived classes? :) Could be useful for a memory manager.
I know this post is well outdated, but I thought I'd add one single word: "Singletons" :)

Cheers!
AL
If you're making a linked list out of only one class you might want to make a private constructor. The private constructor would deal with the creation of nodes, which you don't want the user meddling with, and the public constructor would be used to create a new list. Just a thought.:)