Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

jnkzm

C++ questions

This topic is 5959 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

In C++ you can initilize an object with {}: struct S { int a, b; }; S s = {1, 2}; but how can i do this after adding a constructor? is it possible? Also, i''ve read that i should use ++i instead of i++ in for loops. Could anyone explain this to me?

Share this post


Link to post
Share on other sites
Advertisement
error C2552: ''s'' : non-aggregates cannot be initialized with initializer list

Sounds like a ''no'' to me.

The difference between prefix and postfix incrementation is negligible. Because of their nature, prefix is marginally faster; the only real reason to discriminate between them is their functionality: use whatever makes it easier on you.

Later,
ZE.


//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

Share this post


Link to post
Share on other sites
quote:
Original post by jnkzm
but how can i do this after adding a constructor? is it possible?

Nope, use the constructor instead.

quote:
Original post by jnkzm
Also, i've read that i should use ++i instead of i++ in for loops. Could anyone explain this to me?

i++ involves copying, where ++i does not.

// postincrement
TheClass TheClass::operator++(int)
{
TheClass retVal = *this;
*this = *this + 1;
return retVal;
}

// preincrement
TheClass& TheClass::operator++()
{
*this = *this + 1;
return *this;
}

Anyway i++ involves copying, In the case where a copy is rather costly (as for example in std::rope::iterator) it is not good practice.

EDIT1: Added clearer code example.

[edited by - dalleboy on June 27, 2002 5:58:41 PM]

Share this post


Link to post
Share on other sites
The difference between ++I and I++... depends on what you are using it for.

++I means "first increment, then use" , and I++ means "first use then increment". For loops, it is really not important which one you use.

However, when you are using arrays, you really see why there are two types of increment operators.

take this code for example:
while(i array[i++] = 0;
}

this tells the program to initialize the valaue of the array in the index i to 0, and then increment i. That way you can do two things in one line. If you used ++i instead, it would increment, THEN initialize.

Hope it helped.





Share this post


Link to post
Share on other sites
it IS important which one you use ... EVEN FOR LOOPS ... if the loop index type is a class, not an int.

If the index type is an int ... the compiler will almost always make i++ be ++i, that''s what the optimizer can do for you. But if you write overloaded ++ operators, the optimizer cannot usually detect how to optimize the code, so you must use ++i if you want to ensure good performance. ESPECIALLY when talking about using iterators with the STL, because many of them have a small, but noticable cost with copying, which ++i would avoid.

The {} format initializer list is meant for use with arrays ... i was actually not even aware that the code you posted was legal in the first place (and I''m still not sure it is standard C++, because a struct is NOT an aggregate type ...)

NOW ... when you have an object, like an int, or a class like struct S (with constructor) ... the prefered method of initialization is:

int i(5);
S s(1,2);

and these can be written in alternate form as follows:

int i = 5;
int i = int(5);
// and
S s = S(1,2);

NOTE ... that the {} form of initializing ARRAYS .. is valid for built in types AS WELL AS classes and structs (with contructors).

You can write:

int intArray[4] = {1,2,3,4};
// same as
int intArray[4] = {(1),(2),(3),(4)}
// and
int intArray[4] = {int(1),int(2),int(3),int(4)}

// and i BELIEVE the following 2 work ...
// i KNOW one of them does, but forgot which
S structArray[4] = {(1,2),(10,20),(100,200),(1000,2000)};
// or
S structArray[4] = {S(1,2),S(10,20),S(100,200),S(1000,2000)};

hope that helps.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!