1

I am using new to allocate a memory to a derived class, I also want to initialize its base private member

how can I do that?

class Base {
private:
  int value;
}

class Derived : public Base {
  ....
}

any smart way to use the base constructor? thanks!

Pavel
  • 7,436
  • 2
  • 29
  • 42
YNWA
  • 680
  • 2
  • 9
  • 19
  • 1
    Call the appropriate constructor in the derived class constructor's initializer list. – chris May 24 '14 at 15:41
  • or have a dedicated `init()` function – Pavel May 24 '14 at 15:42
  • You can't, because `Base` has no constructor has no constructor that allows you to set `value`. Also, `new` has no relevance here at all. – juanchopanza May 24 '14 at 15:43
  • 1
    use of `new` and calling base constructors are independent issues. or as we say, they're orthogonal issues. btw note that your class examples are syntactically invalid even when the dots are removed. – Cheers and hth. - Alf May 24 '14 at 15:46
  • @juanchopanza Then why is he asking about "using Base constructor"? I assume he can write a constructor (or at least an initialization method), otherwise the question doesn't make too much sense, as `val` is private in `A` and hence "invisible" to the methods of `B`. – vsoftco May 24 '14 at 15:48
  • @vsoftco You are misquoting OP. The question doesn't make much sense anyway. It is hard to know what people are asking about when so much is wrong. – juanchopanza May 24 '14 at 15:52

3 Answers3

6

Base needs to have a constructor that initializes value, such as

Base(int v):value(v){};

Then, in Derived constructor, you invoke Base constructor as

Derived(int v):Base(v){...};
vsoftco
  • 55,410
  • 12
  • 139
  • 252
6

The constructors of base classes are always called before the constructor of most derived class is called, whether you do it explicitly or not. By default, the default constructor gets called. If you want some other behaviour, you do it in the initialization list:

class Base { 
protected:
    explicit Base(int) {}
};
class Derived : public Base {
public:
    Derived() : Base(42)  // <-- call to base constructor
    { }
};
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
jrok
  • 54,456
  • 9
  • 109
  • 141
0

you can make Drived class friend of the Base class

class Base
{
friend class Drived;
private:
    int a;
};

class Drived :public Base
{
public:
    Drived(){
        this->a=23;
    }
};

Or make variables of base class protected :

class Base
{
protected:
    int a;
};

class Drived :public Base
{
public:
    Drived(){
        this->a=23;
    }
};
uchar
  • 2,552
  • 4
  • 29
  • 50
  • While it works, this is a terrible programming habit: what if you derived Drived? You would have to mark the new class as a friend of both Base and Drived. Imagine what that would do for a big project... – Eternal May 24 '14 at 16:30
  • 1
    Also: if this was in a library, that would mean that your users would have to recompile everything using Base or any of its derived classes each time you add a new class to the inheritance tree... – Eternal May 24 '14 at 16:31