Go Back   ZeroC Forums > Bug Reports

Reply
 
LinkBack Thread Tools Rate Thread Display Modes
  #1 (permalink)  
Old 03-26-2009
JohnB JohnB is offline
Registered User
 
Name: John Basrai
Organization: Northrop Grumman
Project: Airborne Payload Command Control Interface
 
Join Date: Feb 2005
Posts: 39
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
Reply With Quote
  #2 (permalink)  
Old 03-26-2009
bernard's Avatar
bernard bernard is offline
ZeroC Staff
 
Name: Bernard Normier
Organization: ZeroC, Inc.
Project: Ice
 
Join Date: Feb 2003
Location: Palm Beach Gardens, FL
Posts: 904
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
__________________
Bernard Normier
ZeroC, Inc.

Last edited by bernard : 03-26-2009 at 03:32 PM.
Reply With Quote
  #3 (permalink)  
Old 03-26-2009
JohnB JohnB is offline
Registered User
 
Name: John Basrai
Organization: Northrop Grumman
Project: Airborne Payload Command Control Interface
 
Join Date: Feb 2005
Posts: 39
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
Reply With Quote
  #4 (permalink)  
Old 10-16-2009
halfhp halfhp is offline
Registered User
 
Name: Nick Fellows
Organization: Aoptix
Project: optical comms appliance
 
Join Date: Oct 2009
Posts: 13
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?
Reply With Quote
  #5 (permalink)  
Old 10-16-2009
bernard's Avatar
bernard bernard is offline
ZeroC Staff
 
Name: Bernard Normier
Organization: ZeroC, Inc.
Project: Ice
 
Join Date: Feb 2003
Location: Palm Beach Gardens, FL
Posts: 904
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.
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
enumerate derived class names of a given slice class peterlspot Comments 1 11-29-2007 08:26 AM
Helper Classes not Generated zhi Help Center 5 01-04-2007 08:18 AM
Base Class For Ref-Counted Classes? acbell Help Center 2 03-06-2006 06:32 PM
generated C# classes don't have constructors with arguments for fields kovacm Bug Reports 1 08-07-2005 08:00 PM
bug when pass a class by value damingyipai Bug Reports 2 04-02-2004 03:51 AM


All times are GMT -4. The time now is 04:08 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
(c) 2009 ZeroC, Inc.