Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Initialization before inheritance

inheritance

  • Please log in to reply
8 replies to this topic

#1 aftos

aftos

    CC Newcomer

  • Member
  • PipPip
  • 19 posts

Posted 28 March 2011 - 02:36 AM

Hi,
i have a problem with initialization. Is it possible to initialize a value before initialization of the base class? What i mean is, let's say we have class A and class B which inherits A:

class A:
{
    public:
       virtual void fun1();
}

class B : A
{
    public:
        virtual void fun1();

    protected:
        int val;
}

A::A()
{
     fun1();
}

void B:fun1()
{
    if (val) do_something;
}

Now, when i create a new object of "B" (new B), i need to initialize "val" before the initialization of the base class "A", because "fun1" is called during initialization of "A" which is virtual, so "fun1" of "B" will be called. But in "fun1" of "B" there's a control "if (val) etc" which results in unitialized junk!
I tried to initialize the value in the constructor like this:
B::B() : val = 0, A()
but i get a warning that "val" will be initialized after initialization of base class. Ok, it seems logical to work like this, but in my case... how can i resolve this?
Thanx in advance :c-cool:

Edited by dargueta, 28 March 2011 - 01:33 PM.
Please use code tags next time.

  • 0

#2 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 28 March 2011 - 06:39 AM

Variable val in B is not visible in A (unless I'm missing something) and you can not init it in A. Simply init val in B's constructor and it should be fine.
  • 0

The roots of education are bitter, but the fruit is sweet.


#3 aftos

aftos

    CC Newcomer

  • Member
  • PipPip
  • 19 posts

Posted 28 March 2011 - 10:38 PM

Ok, "val" is not visible in "A" but this is not a problem. As you can see in the code above, in the constructor of "A" is called the virtual function "fun1". So, if you do:
new B(), the inherited class will be called first. So, in this example, the initialization goes like this:
1) Initialization of A.
2) In "A", is called the function fun1, which is virtual.
3) B::fun1() will be called.
4) Initialization of B.
As you can see, even if i initialize "val" in B, it will be set up later!

EDIT: I'll try to set a flag in base class which will tell me if i'm in constructor "mode" or not ... maybe :confused:
  • 0

#4 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 29 March 2011 - 04:51 AM

3) B::fun1() will be called.

Where does this get called? You can explicitly tell which method should be called with scope operator:
B::B()
    : val(0)
{
    B::fun1();
}
Is this what you meant?
  • 0

The roots of education are bitter, but the fruit is sweet.


#5 aftos

aftos

    CC Newcomer

  • Member
  • PipPip
  • 19 posts

Posted 30 March 2011 - 07:24 AM

Maybe i confused you because i didn't write any code about "B"'s constructor. Let's simply say:

B::B() : A()

Normally, there are some values i want to pass from "B" to "A", that's why i put "A" in the end. B::fun1() is being called in constructor of "A". It's actually written "fun1()" but it's a virtual function so, in our case, will be called fun1 from class "B". I think there's no direct solution to this. It's a design problem.
  • 0

#6 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 30 March 2011 - 07:55 AM

B::fun1() is not called in A's ctor, A::fun1() is called in A(). If you wanna pass from B to A, make ctor in A that matches whatever you wanna send over.
  • 0

The roots of education are bitter, but the fruit is sweet.


#7 aftos

aftos

    CC Newcomer

  • Member
  • PipPip
  • 19 posts

Posted 31 March 2011 - 11:43 PM

My bad! In my program's classes (a lot more complicated), during initialization, it's being called a method of a "B" which cause the whole problem. By the way, i thought that if i create a "B", in constructor will be called the virtual function, which is not happening. Thanks a lot!
  • 0

#8 Zer033

Zer033

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 84 posts

Posted 01 April 2011 - 06:03 AM

Are you talking about this:


class A
{
virtual void func();
A(int);
int val;
};

class B : A
{
B()
virtual void func();
};

A::A(value)
{
val = value;
}
B::B() : A(value)
{

}

Sorry in a hurry, but is this kinda what you were asking or talking about?
  • 0

#9 aftos

aftos

    CC Newcomer

  • Member
  • PipPip
  • 19 posts

Posted 03 April 2011 - 09:36 PM

No. This is different. Anyway, i was wrong about calling of virtual function, so the whole post is an error :P
  • 0





Also tagged with one or more of these keywords: inheritance