Skip to main content
GameDev.net gamedev.net
🔒 Locked

Constructors in struct's

Started by GekkoCube Mar 19, 2003 at 5:35 AM 4 replies 150+ views
Original Post
GekkoCube
GekkoCube
is it legal to have constructors (even overloaded) in a struct? i tried this with no errors, but when i change something that is remotely related to this, i get weird errors. i had something to this effect...
  
struct Vertex {
  Vertex( float px, float py float pz ) {
    x = px;
    y = py;
    z = pz;
  }
  Vertex() {
    x = y = z = 0.0f;
  }
  
  float x, y, z;
  float tu, tv;
};
  
Kylotan
Kylotan
Totally legal in C++. However one could reasonably argue that structs should be kept free of data members, so that a C++ struct is functionally identical to a C struct. Especially since there''s never really any reason to call it a struct anyway.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost

Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
Eskhan
Eskhan
Not really related, but can you inherit from a struct now that they can contain member functions and data?
--------------------Though this program be madness, yet there is a method in't
civguy
civguy
I''m still waiting for that FAQ. Any moderator could put an announcement with answers to stuff like this..
Anon Mike
Anon Mike
The only difference between a struct and a class in C++ is that a struct defaults members to public and a class defaults to private.

So yes, a struct can have constructors/destructors, virtual functions, you can inherit from them, etc., etc. Whether you use one or another is just a matter of style and personal taste.

And no, to answer the inevitable next question, there is no speed difference.
-Mike
Russell
Russell
quote:
And no, to answer the inevitable next question, there is no speed difference.
Between what?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.