Base Class Constructor parameter has a struct

This is a discussion on Base Class Constructor parameter has a struct within the C++ Programming forums, part of the General Programming Boards category; Hi All, I want to know if the following is possible. Code: struct MyStruct { int x; } ; class ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    Base Class Constructor parameter has a struct

    Hi All,

    I want to know if the following is possible.

    Code:
    struct MyStruct
    {
     int x;
    } ;
    
    class AdvancedClass
    {
       AdvancedClass( MyStruct myStruct ) ;
    };
    
    class BasicClass : public AdvancedClass
    {
     BasicClass(void) ;
    };
    
    BasicClass::BasicClass(void) : AdvancedClass( {0} )  // doesnt compile
    {
    
    }
    As you can see from this simple code the Advanced Base Class expects a MyStruct as a constructor parameter. The derived Basic class doesnt have any constructor paramets and tries to constuct an AdvancedClass with a MyStruct object that has an x value of 0.

    The problem is that I dont know how to initialize a new MyStruct object inside the creation of the AdvancedClass object from the initialization list of the BasicClass object.

    Any ideas?

    Thanks

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Try this.
    Code:
    BasicClass::BasicClass(void) : AdvancedClass( (MyStruct){0} )
    It should work . . . Dinkumware doesn't mind it, and it's valid C99. (The cast, not the class stuff.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Doesn't compile at all with VC++.
    Alternatively, make MyStruct a class and make a constructor that takes an int.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    Thanks for the reply mate,

    I get the following error when i try it:

    syntax error : missing ')' before '{'

    If it makes any difference the MyStruct object is actually in the form of

    Code:
    strut MyStruct
    {
     float x;
     float y;
     float z;
    };
    So the code is actually

    Code:
    BasicClass::BasicClass(void) : AdvancedClass( (MyStruct){0.0f,0.0f,0.0f} )
    Sorry for the misleading example, the former one was quicker to type and i didnt think it would make a difference

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,032
    Just put it into the constructor body. For POD types there is no loss from doing this. I doubt what dwks sais is valid C++. C++0x maybe, but new C99 features where never incorporated into C++.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Doesn't compile at all with VC++.
    I doubt what dwks sai[d] is valid C++. C++0x maybe, but new C99 features [were] never incorporated into C++.
    Hmm, you're right. Must be a GNU extension. Not surprising, really.
    Alternatively, make MyStruct a class and make a constructor that takes an int.
    You can even do that for a structure. Structures (in C++, anyway) can have constructors, destructors, private and public labels, the whole works. In C++, the only difference between a struct and a class is that structs are public by default, and classes private.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    thanks dwks i didnt know a structure could behave like that. It is compiling now

    Thanks to the rest of you aswell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning class and struct members
    By simone.marras in forum C++ Programming
    Replies: 17
    Last Post: 03-16-2009, 11:10 AM
  2. How to copy a C struct to a C++ class?
    By Ptbamboo in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2009, 01:11 PM
  3. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  4. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21