Thread: references and multiple inheritance - incompatible?

  1. #1
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838

    references and multiple inheritance - incompatible?

    is it possible to use a class with reference type members? it seems not since classes using multiple inheritance must have a default constructor, which leaves the references uninitialized, or at best incorrectly initialized.

    if there's a way, someone please show how!

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What is the reference supposed to point to?
    If that's not working, why not use a pointer instead?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are you sure that "classes using multiple inheritance must have a default constructor"? Methinks that the derived class ctor can initialise the base class subobjects via its initialisation list. Of course, even if what you say is true, there is no reason that classes with reference members must be part of a multiple inheritance hierarchy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by laserlight
    Methinks that the derived class ctor can initialise the base class subobjects via its initialisation list.
    ah-ha...i will try this.


    i am just asking; i haven't actually encountered such a need to use references in a multiply derived object.

    i had an unneeded virtual keyword in a class definition. when i went to derive it, i ran across this problem and i didn't see any immediate way around it other than to nix the virtual keyword (which i should have anyway i suppose).

    thanks for the comments. i'll try that if i need to. i was just curious if there was an obvious solution i was overlooking, and it appears that was probably the case.

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    ok, i looked more closely and it still does not compile if the derived ctor initializes the reference of the base class.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Show your code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Code:
    __fastcall analysisModule::analysisModule(process *owner,String mName,spectrometer &Sensor,int h,int w) :
            procObj(),
            onLoadEqnSet(taskArray(mName+" On Load New Eqn Set")),
            firstLoad(true),
            sensor(Sensor),
            applyData(false)
            {
    
                    init(owner,mName,h,w);
            }
    
    __fastcall analysisModule::analysisModule() :
            procObj(),
            onLoadEqnSet(taskArray("Analysis Module: On Load New Eqn Set")),
            firstLoad(true)
            {
    
            }
    sensor is not initialized in the default ctor

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you probably want a pointer to Sensor rather than a reference to Sensor (or you want a "DummySensor" that you can set Sensor to in the case where you don't use a sensor).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    yes, i used a pointer before in fact, but i have been trying to optimize where possible to use references instead of pointers.

    the case of virtual inheritance is the first time i have encountered a situation were they were not interchangeable.
    Last edited by m37h0d; 09-03-2008 at 10:25 AM.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To the extent of my knowledge, a class can only initialize its own members via the initializer list.
    What you can do is create a constructor for the base class to initialize the reference and call this base class constructor via the initializer list instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by m37h0d View Post
    yes, i used a pointer before in fact, but i have been trying to optimize where possible to use references instead of pointers.

    the case of virtual inheritance is the first time i have encountered a situation were they were not interchangeable.
    There is also no benefit between a reference or pointer with regards to performance. Under the hood [1] a reference becomes a pointer - the only difference is in the fact that a pointer can be assigned at any time, and syntactically it "looks different".



    [1] Except for a few instances where the compiler can figure out what the reference is actually referring to and "optimize away the whole reference variable alltogether".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    yes, i meant optimization for safety more than speed, but i generally do a good job of making sure my pointers point to things (or are assigned NULL)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Splitting source into multiple files(Linux & make)
    By IceDane in forum C Programming
    Replies: 6
    Last Post: 05-18-2009, 07:31 AM
  2. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 08:11 PM