Sign in to follow this  

two very simple question with OOP thank advance

This topic is 4098 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

{ public: bool Windowed; uint16 Width; uint16 Height; uint8 Depth; uint Frequency; /////////////////////////////// why this cmode function havent return type? CMode(void) { Windowed=false; Width=0; Height=0; Depth=0; Frequency=0; } CMode(uint16 w, uint16 h, uint8 d, bool windowed= true, uint frequency = 0) { Windowed=windowed; Width=w; Height=h; Depth=d; Frequency=frequency; } }; //////////////////////////////////////////why these function had assign = 0; ??? virtual void flush() = 0; /** set the number of VBL wait when a swapBuffers() is issued. 0 means no synchronisation to the VBL * Default is 1. Values >1 may be clamped to 1 by the driver. */ virtual void setSwapVBLInterval(uint interval) =0; /// get the number of VBL wait when a swapBuffers() is issued. 0 means no synchronisation to the VBL virtual uint getSwapVBLInterval() =0;

Share this post


Link to post
Share on other sites
CMode::CMode() is what's called the constructor of the object. This is a function that is called when an instance of that object is created. Since it's frequently used like this:

CMode *mode = new CMode(); < -- That calls the constructor!

It can't return a type, not even void, so it doesn't have a type. Same goes for the destructor, which is ~CMode() and is called when an object is deleted or goes out of scope.

Why some functions are being assigned to 0 is a bit of a C++ quirk. Only functions defined (at least AFAIK) with the virtual keyword can have this. This means that the function is pure virtual. This means that later on, when another class inherits from CMode, it will write that function, but until then, CMode simply doesn't have that function. This has the side effect of making it that you can't have instances of CMode. Period. However, it's now an abstract class (one that you can't make instances of, because it's not complete (ie: the missing function)) and classes that inherit from it will be able to be made.

I'm guessing that most of this is beyond what you have learned so far (especially the part about the pure virtual functions). That's fine, you'll learn it eventually. It's good to see that you've noticed these odd things instead of just blindly copy-and-pasting the code knowing that "it just works".

Share this post


Link to post
Share on other sites
Neither is CMode() a function, nor is the =0 an assignment in traditional sense.

The first one is the syntax for a "constructor", i.e. a method that initializes the state of the object just after its allocation. A constructor is named like the class/struct it initializes. There is also the opposite, a "desctructor", to be determined that is is written like a contructor but with a leading tilde ~ (and, moreover, a desctructor must not have arguments).

And the other thing: It means that the function flush() is not only virtual but "pure virtual". That means that at this point of code only the function's declaration but not its implementation (i.e. function body) is known. Look up for "polymorphism" and "pure virtual functions".

Share this post


Link to post
Share on other sites

class CMode
{
public:
bool Windowed;
uint16 Width;
uint16 Height;
uint8 Depth;
uint Frequency; /////////////////////////////// why this cmode function havent return type?
CMode(void)
{
Windowed=false;
Width=0;
Height=0;
Depth=0;
Frequency=0;
}
CMode(uint16 w, uint16 h, uint8 d, bool windowed= true, uint frequency = 0)
{
Windowed=windowed;
Width=w;
Height=h;
Depth=d;
Frequency=frequency;
}
};


ok just tidied that up and think class CMode was missing at the start, when you post code you can put it beetween [code]
 or 

[/code] and it stays readable.

Now to answer you questions, the CMode() function doesnt have a return type because its the constructor for the class i.e it doesnt have to return anything (not even void) because its job is soly to initialize the CMode class. Second the = 0 is theire to say "i'm not going to write any code for this function write now when someother class inherits from this one it can write the flush() function instead"

Hope that helps

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
why this cmode function havent return type?

CMode is a constructor, which do not return a type; the only thing that a constructor may return is an error via "throw".

Quote:
why these function had assign = 0;

This means its a pure virtual function that a derived class must define. Due to this being pure virtual an instance of the class can not be created.

Have a look at the C++ course which is ongoing on this board, it may help your understanding of C++.
http://www.gamedev.net/community/forums/forum.asp?forum_id=76

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Julian90
lol seems everyone said same thing at same time lol


oops :)

Share this post


Link to post
Share on other sites
thanks thanks thanks too much every goodness people~ im be respectful readed every post at here~ thanks everyone again~ again!

i was read the cpp book~ but my english ability not well~ so i cant figer that out easily~

Share this post


Link to post
Share on other sites

This topic is 4098 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this