a class in a struct?

This is a discussion on a class in a struct? within the C++ Programming forums, part of the General Programming Boards category; Basically what you are doing is calling the constructor for dice and you don't need to. Are you sure about ...

  1. #16
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Basically what you are doing is calling the constructor for dice and you don't need to.
    Are you sure about that? As I recall from some book somewhere (I'm sure everyone knows what book I'm talking about ), struct is 99% the same as class except that things are public by default, so it actually looks to me like your declaring a function called dice() that returns a diceClass.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #17
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    Are you sure about that?
    Damn your right. Its not a constructor call.I'm not sure what my compiler is seeing it as but it sure isn't a class. Will the compiler see it as a function that takes an unknown number of arguments? The only good thing is that when you try to use it, it will fail. I tried this code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class barClass
    {
       public:
          void hello()
          {  cout<<"Hello. This is a test.\n";   }
    };
    
    struct Foo
    {
       public:
          barClass bClass();
    };
    
    int main(int argc, char *argv[])
    {
       Foo fooFoo;
    
       fooFoo.bClass.hello();
    
       return(0);
    }
    and got the compiler error telling me:
    error C2228: left of '.hello' must have class/struct/union type
    So it looks like you are right Hunter2. I though that declaring a function without any arguments is only allowed in C.
    Last edited by Dohojar; 09-10-2003 at 12:51 AM.
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  3. #18
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I though that declaring a function without any arguments is only allowed in C.
    Now how this might happen, I'm not sure Unless you mean "int function(void);". If you want to declare an object of barClass, you simply go "barClass bClass;" and it will automatically use the default constructor (i.e. the one that has no arguments), unless the Foo constructor explicitly initializes it using one of the constructors that takes arguments. Example:
    Code:
    Foo::Foo()
    :bClass(theArgumentYouWantToUse)
    {
        //do something
    }
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #19
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    Now how this might happen, I'm not sure Unless you mean "int function(void);
    No I mean int function() or are you telling me that a function with no arguments specified defaults to void in c++?(Just as an added note, VC++ sees it as void).I just want this cleared up so I know what the standard is for function prototypes in c++ because right now I am under the impression that all functions have to specify an argument even if it is void.


    I did some searching and found out that in C
    int function( );
    is deemed to take an unknown number of arguments.(from the FAQ on this site) But this same statement is interpreted in C++ as a prototype equivalent to the following:
    int function(void );
    Guess thats what happens when you always prototype functions. I had forgotten all about that but there it was in black and white in one of my c++ programming books.
    Last edited by Dohojar; 09-11-2003 at 09:25 AM.
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  5. #20
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    C does (or at least did) have that convention, where
    int f( ) is unknown number of args
    int f(...) is the C++ equivalent

    In C++,
    int f( ) and
    int f(void)
    are the same.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #21
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Really? I didn't know that. I always thought that not specifying any arguments meant just zero arguments for both languages... I learn something new every day
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #22
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    Looks like we both did. I always thought that in c++ all function arguments had to be specified, even if the dont' take any.
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  2. const elements of struct / class not accessible?
    By talz13 in forum C# Programming
    Replies: 2
    Last Post: 03-24-2006, 04:05 PM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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