Invoking a base class constructor

Discussion in 'C++' started by Dominique, Nov 15, 2003.

  1. Dominique

    Dominique Guest

    Suppose I have:

    class A
    {
    A(int x, int y);
    };

    and
    class B: public A
    {
    .... some declarations
    };

    then I try:
    int s, t;
    B bObject(s, t);

    I get a compiler error (gcc) that no constructor of the form B(int&, int&)
    exists. However, I would have expected that the constructor from class A
    would be invoked (since B 'is a' A). Adding a B(int x, int y), and using
    that to invoke A solves the problem.

    Am I doing something wrong?

    Thanks
    Dominique
    Dominique, Nov 15, 2003
    #1
    1. Advertising

  2. Dominique

    Ron Natalie Guest

    "Dominique" <NODOTd.b.g.e.n.e.r.a.l.0.1.LOSEDOTS@cogecoDOTca> wrote in message news:KIutb.2121$_%...

    > I get a compiler error (gcc) that no constructor of the form B(int&, int&)
    > exists. However, I would have expected that the constructor from class A
    > would be invoked (since B 'is a' A). Adding a B(int x, int y), and using
    > that to invoke A solves the problem.
    >
    > Am I doing something wrong?

    Constructors don't get inheritted. The only constructors that exist for B
    are the two implicitly generated ones:
    B()
    and B(const B&)

    If you want one that takes two ints and passes them along as initailizers to
    the A object you need to write:

    B(int s, int t) : A(s,t) { }

    Note that this will inhibit the creation of the B() constructor. If you need one, you
    will have to provide it.
    Ron Natalie, Nov 15, 2003
    #2
    1. Advertising

  3. Dominique

    amit gulati Guest

    Ron Natalie wrote:
    > "Dominique" <NODOTd.b.g.e.n.e.r.a.l.0.1.LOSEDOTS@cogecoDOTca> wrote in message news:KIutb.2121$_%...
    >
    >
    >>I get a compiler error (gcc) that no constructor of the form B(int&, int&)
    >>exists. However, I would have expected that the constructor from class A
    >>would be invoked (since B 'is a' A). Adding a B(int x, int y), and using
    >>that to invoke A solves the problem.
    >>
    >>Am I doing something wrong?

    >
    > Constructors don't get inheritted. The only constructors that exist for B
    > are the two implicitly generated ones:
    > B()
    > and B(const B&)
    >
    > If you want one that takes two ints and passes them along as initailizers to
    > the A object you need to write:
    >
    > B(int s, int t) : A(s,t) { }
    >
    > Note that this will inhibit the creation of the B() constructor. If you need one, you
    > will have to provide it.
    >
    >


    That is not completely true, constructors do get inherited. The problem
    with the code is that, since class A defines a non default cnstructor
    A(int, int) the compiler does not generate a default constructor. Hence,
    A() doesnot exist.

    In B(int, int), since you are not explicitly calling A(int, int), the
    compiler internally (while compiling) adds A() to the B(int, int)
    constructor. But A() doesnot exists, hence you get the error message.

    Good explanation of this phenomenon is present in the excellent book
    "C++ object model" by Stanley lippman.
    amit gulati, Nov 15, 2003
    #3
  4. Dominique

    Ron Natalie Guest

    "amit gulati" <> wrote in message news:bp602l$7dj$...

    > That is not completely true, constructors do get inherited.


    No they don't. They have no name, they don't participate in lookup, so how
    does the C++ notion of inheritance.

    > The problem
    > with the code is that, since class A defines a non default cnstructor
    > A(int, int) the compiler does not generate a default constructor. Hence,
    > A() doesnot exist.


    This wasn't what he was asking. He wanted to know why he could construct
    a B with two ints. The reason is that there is no B constructor that takes two
    ints. Go back and read the original post.
    Ron Natalie, Nov 15, 2003
    #4
  5. Dominique

    Rolf Magnus Guest

    amit gulati wrote:

    > That is not completely true, constructors do get inherited.


    No, they don't.

    > The problem with the code is that, since class A defines a non default
    > cnstructor A(int, int) the compiler does not generate a default
    > constructor. Hence, A() doesnot exist.


    Right.

    > In B(int, int),


    B(int, int) doesn't exist.

    > since you are not explicitly calling A(int, int), the
    > compiler internally (while compiling) adds A() to the B(int, int)
    > constructor. But A() doesnot exists, hence you get the error message.


    He didn't get an error message that A() doesn't exist, but one that
    B(int, int) doesn't exist, which is true.
    Just try out what happens if you add a default constructor to A.
    According to your explanation, it should work then.

    > Good explanation of this phenomenon is present in the excellent book
    > "C++ object model" by Stanley lippman.


    Never heared of that book. Seems that it belongs on the
    not-to-be-recommended list.
    Rolf Magnus, Nov 16, 2003
    #5
  6. Dominique

    amit gulati Guest

    amit gulati wrote:
    > Ron Natalie wrote:
    >
    >> "Dominique" <NODOTd.b.g.e.n.e.r.a.l.0.1.LOSEDOTS@cogecoDOTca> wrote in
    >> message news:KIutb.2121$_%...
    >>
    >>
    >>> I get a compiler error (gcc) that no constructor of the form B(int&,
    >>> int&)
    >>> exists. However, I would have expected that the constructor from class A
    >>> would be invoked (since B 'is a' A). Adding a B(int x, int y), and using
    >>> that to invoke A solves the problem.
    >>>
    >>> Am I doing something wrong?

    >>
    >>
    >> Constructors don't get inheritted. The only constructors that exist
    >> for B
    >> are the two implicitly generated ones:
    >> B()
    >> and B(const B&)



    What I mean by inherited is that in class B you can call A's
    constructors, doesn't that mean that the constructors are inherited. I
    never said that if you define A(int, int) then the compiler will
    generate B(int, int).

    Inheritance means that the properties (data and functions) of the base
    class are available to the sub class as if they were it's own.

    Hence anywhere in B I can call A(int, int), this shows that the
    constructor A(int, int) is inherited.

    I agree I did not read the problem that the person had completely. But
    the fact is constructors are inherited.

    >>
    >> If you want one that takes two ints and passes them along as
    >> initailizers to
    >> the A object you need to write:
    >>
    >> B(int s, int t) : A(s,t) { }
    >>
    >> Note that this will inhibit the creation of the B() constructor. If
    >> you need one, you
    >> will have to provide it.
    >>
    >>

    >
    > That is not completely true, constructors do get inherited. The problem
    > with the code is that, since class A defines a non default cnstructor
    > A(int, int) the compiler does not generate a default constructor. Hence,
    > A() doesnot exist.
    >
    > In B(int, int), since you are not explicitly calling A(int, int), the
    > compiler internally (while compiling) adds A() to the B(int, int)
    > constructor. But A() doesnot exists, hence you get the error message.
    >
    > Good explanation of this phenomenon is present in the excellent book
    > "C++ object model" by Stanley lippman.
    >
    amit gulati, Nov 17, 2003
    #6
  7. Dominique

    Ron Natalie Guest

    "amit gulati" <> wrote in message news:bpak8q$a11$...
    =
    > What I mean by inherited is that in class B you can call A's
    > constructors, doesn't that mean that the constructors are inherited.


    Well, you're still wrong. You can't call constructors at all.

    > Inheritance means that the properties (data and functions) of the base
    > class are available to the sub class as if they were it's own.


    Which does not happen for constructors.

    > Hence anywhere in B I can call A(int, int), this shows that the
    > constructor A(int, int) is inherited.


    You can't call A(int,int) anywhere. You can't call constructors.
    Ron Natalie, Nov 17, 2003
    #7
  8. Dominique

    amit gulati Guest

    Ron Natalie wrote:

    > "amit gulati" <> wrote in message news:bpak8q$a11$...
    > =
    >
    >>What I mean by inherited is that in class B you can call A's
    >>constructors, doesn't that mean that the constructors are inherited.

    >
    >
    > Well, you're still wrong. You can't call constructors at all.
    >
    >
    >>Inheritance means that the properties (data and functions) of the base
    >>class are available to the sub class as if they were it's own.

    >
    >
    > Which does not happen for constructors.
    >
    >
    >>Hence anywhere in B I can call A(int, int), this shows that the
    >>constructor A(int, int) is inherited.

    >
    >
    > You can't call A(int,int) anywhere. You can't call constructors.
    >
    >
    >

    OOps! I messed up again.
    Refered to the C++ annotated reference manual page 263

    "Constructors are not inherited"

    Thanks for clearing my doubts.
    amit gulati, Nov 17, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mrstephengross
    Replies:
    5
    Views:
    586
    Larry I Smith
    May 18, 2005
  2. hurcan solter

    Invoking A Base class constructor

    hurcan solter, Jan 7, 2007, in forum: C++
    Replies:
    3
    Views:
    392
    Grizlyk
    Jan 12, 2007
  3. ali
    Replies:
    4
    Views:
    560
    David Harmon
    Mar 5, 2007
  4. Rahul
    Replies:
    16
    Views:
    1,179
    terminator
    Nov 7, 2007
  5. Lilith

    Invoking base constructor

    Lilith, Dec 12, 2007, in forum: C++
    Replies:
    1
    Views:
    320
    Victor Bazarov
    Dec 12, 2007
Loading...

Share This Page