Thread: question about struct

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    question about struct

    Hallo,

    Tried searching, but couldnt find anything, so here it goes:

    Is there a way to initialise a struct in a function like you can with a class?
    Code:
    struct point
    {
    float x,y,
    }
    
    someFunction(point(30,30));  <---Does not work
    Thanks,

  2. #2
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Yes. In C++, a struct is almost identical to a class, the only difference is that by default the members of a struct are public while the members of a class are private. You can write a constructor for your struct just like you would for the class.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  3. #3
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    [edit]Removed code that is entirely incorrect in the C++ forum, but would have been pretty cool in the C forum
    I'm going to bed... way too late[/edit]
    Last edited by Brad0407; 02-23-2008 at 11:20 PM.
    Don't quote me on that... ...seriously

  4. #4
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    structs can certainly have constructors, destructors and member functions in C++ (they can not in C).

    in fact, so long as you are careful to specify public/private appropriately, you could go through a C++ program and replace the word class with the word struct and the program would compile exactly the same.
    Last edited by jEssYcAt; 02-23-2008 at 11:19 PM.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Brad that is not a correct statement for C++. In C++ a struct is exactly the same as a class except for struct is default to public scope, and class is default to private.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Struct question... difference between passing to function...
    By Sparrowhawk in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2009, 03:59 PM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM