Results 1 to 5 of 5

Thread: Generated constructors in derived classes do not pass params correctly to base class

  1. #1
    JohnB is offline Registered User
    Name: John Basrai
    Organization: Northrop Grumman
    Project: Airborne Payload Command Control Interface
    Join Date
    Feb 2005
    Posts
    49

    Generated constructors in derived classes do not pass params correctly to base class

    Hello,

    When I pass a parameter through a base class constructor it does not seem to get set into the base class member variable. Here is a small repo case to demo
    the problem. (Ice version is 3.3.0 for C++)


    Code:
    --- demo.ice. ---
    module demo
    {
        enum ShapeType {
            Unknown,
            RectangleType,
            TriangleType,
            CircleType,
            SquareType
        };
        class Shape {
            ShapeType type;
        };
        class Circle extends Shape {
            int radius;
        };
        sequence<Shape> ShapeSeq;
    };
    
    -- demoTest.cpp --
    #include "demo.h"
    #include <iostream>
    #include <assert.h>
    
    using namespace std;
    using namespace demo;
    
    class CircleI : virtual public Circle
    {
    public:
        CircleI(int rad)
            : Circle(CircleType, rad)
        {
            cerr << "Circle of radius:" << radius 
                   << " type:" << type << endl;
            assert(type == CircleType);
        }
    };
    
    int main(int,char *[])
    {
        ShapeSeq shapes;
        shapes.push_back( new CircleI(20) );
    }
    When I run this I get the following result.

    ./demo
    Circle of radius:20 type:0
    Assertion failed: type == CircleType, file demoTest.cpp, line 15

    Compilation abort (core dumped) at Thu Mar 26 10:38:23
    John Basrai
    Software Engineer
    Northrop Grumman Mission Systems/ISD

  2. #2
    bernard's Avatar
    bernard is offline ZeroC Staff
    Name: Bernard Normier
    Organization: ZeroC, Inc.
    Project: Ice
    Join Date
    Feb 2003
    Location
    Palm Beach Gardens, FL
    Posts
    1,270
    Hi John,

    This is expected, because the generated Circle derives from the generated Shape with virtual inheritance.

    With such virtual base class, the constructor of the most derived class needs to initialize each virtual base class, e.g. you would write:

    Code:
    CircleI(int rad)
            : Shape(CircleType),
              Circle(CircleType, rad)
        {
        }
    Otherwise, it's the default Shape constructor that gets called.

    BTW if you add a third class in your Slice class hierarchy and read the generated code, you'll see that the "one-shot" constructor of the most derived class properly calls the constructor of all base classes with data members.

    Best regards,
    Bernard
    Last edited by bernard; 03-26-2009 at 03:32 PM.
    Bernard Normier
    ZeroC, Inc.

  3. #3
    JohnB is offline Registered User
    Name: John Basrai
    Organization: Northrop Grumman
    Project: Airborne Payload Command Control Interface
    Join Date
    Feb 2005
    Posts
    49

    Thumbs up

    Thanks it works now!

    ./demo
    Circle of radius:20 type:3
    It seems strange to call two base class constructors but it is better than than setting it in the body of the derived constructor which was the only work around that I had.

    Thanks for the prompt reply,
    -john

  4. #4
    halfhp is offline Registered User
    Name: Nick Fellows
    Organization: Aoptix
    Project: optical comms appliance
    Join Date
    Oct 2009
    Posts
    31
    This seems like a really bad thing from the perspective of avoiding duplicate code...now instead of having constructors that neatly cascade, I have a mountain of redundant initialization code that grows with every new class specialization.

    Is there no way around this?

  5. #5
    bernard's Avatar
    bernard is offline ZeroC Staff
    Name: Bernard Normier
    Organization: ZeroC, Inc.
    Project: Ice
    Join Date
    Feb 2003
    Location
    Palm Beach Gardens, FL
    Posts
    1,270
    Hi Nick,

    There is no way-around, as it is a consequence of mapping Slice class inheritance to virtual inheritance in C++. We've changed this mapping in the upcoming 3.4 release (to use non-virtual inheritance by default) to avoid this often surprising behavior.

    Best regards,
    Bernard
    Bernard Normier
    ZeroC, Inc.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 11-29-2007, 08:26 AM
  2. Helper Classes not Generated
    By zhi in forum Help Center
    Replies: 5
    Last Post: 01-04-2007, 08:18 AM
  3. Base Class For Ref-Counted Classes?
    By acbell in forum Help Center
    Replies: 2
    Last Post: 03-06-2006, 06:32 PM
  4. Replies: 1
    Last Post: 08-07-2005, 08:00 PM
  5. bug when pass a class by value
    By damingyipai in forum Bug Reports
    Replies: 2
    Last Post: 04-02-2004, 03:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •