C++, ' ** ' and Graphics Engines...

Started by
20 comments, last by hplus0603 18 years, 3 months ago
Quote:Original post by webwraith
I'd have thought that 1.0 would have been treated as a float?
I take it, you only need to add the <..> when casting as a new type?

Thanks for the help, so far.` I'm all ears for any other suggestions

1.0 is treated as a double by default (by Visual Studio.NET, at least). 1.0f is treated as a float.

You actually need to add the < > every time you call the function, if you're not sending a value from a variable.

EDIT: Made another change above, something wasn't right.

[Edited by - iNsAn1tY on December 31, 2005 9:10:08 AM]
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]
Advertisement
Just to make sure you're clear on this...

template <class T>inline T sqr(const T& x){ return x*x; } int ix = 1;float fx = 1.0f;double dx = 1.0;sqr(ix); // compiler generates:  inline int sqr_1(const int& x){return x*x;} sqr(fx);// compiler generates: inline float sqr_2(const float& x) {return x*x;}sqr(dx);// compiler generates: inline double sqr_3(const double& x) { return x*x;}


Now here's a question for someone more C++ savvy than me...
Does passing by const reference instead of by value lose performance?
Will the program have to dereference x inside sqr? Is passing by value faster for POD types?

-Alex
I wouldn't know, I'm not even sure what the difference between a reference and a pointer is. when I first heared about them, I thought they were the same thing with different names
Another thing, if you were trying to cast as a different type, would you use <..> or would you add the cast inside the brackets? Or both?

Finally (phew!) would this need to go in the header, the associated source, or my main source?
Quote:Original post by webwraith
I wouldn't know, I'm not even sure what the difference between a reference and a pointer is. when I first heared about them, I thought they were the same thing with different names


a reference is a pointer without all the fancy sytax for accesses.

eg

void changeToFive( int* variable ){   *variable = 5; // note the dereference operator...}void changeToFive( int& variable ){   variable = 5;}class SomeClass{public:void someMethod();};void someFunction( SomeClass *someObject ){    someObject->someMethod();//more funky syntax to be aware of}void someFunction( SomeClass &someObject ){    someObject.someMethod();//same thing, but clearer}


pointers can do more, pointer math, have NULL pointers, used for dynamilcally getting memory. references are just easy-on-the-eyes limited pointers.
Quote:
Another thing, if you were trying to cast as a different type, would you use <..> or would you add the cast inside the brackets? Or both?

Finally (phew!) would this need to go in the header, the associated source, or my main source?


Let's say you wanted to generate sqr(double) even though you were passing the function an integer.

Calling sqr( (double) ix ) will accomplish this.

-Alex
Thanks, cipherx, and I'd assume the answer to your question is that passing a value would be quicker, though I don't imagine there would be much of a difference.

Don't quote me on that though...
Calling it by
sqr<double>(someint)
is correct. Cypher's will work, but is containes a nasty little c-style cast.
what's wrong with C-style casts?
Answer 1:
Passing by const reference to a non-inline function may lose performance, because the piece of data needs to live on the stack (so that a pointer/reference can be generated). If the function is inline (such as most templates) then the compiler has enough information to optimize the passing to actually be by value -- although whether it chooses to do that may vary based on the compiler and specific compiler flags. If you know it's only for ints, floats, doubles and the like, then pass by value.

Answer 2:
In the case of a downcast, C-style casts will perform a static_cast<> when the base type is known, but a reinterpret_cast<> when the base type is not known, without telling you about it. static_cast<> will tell you (using an error) if you're trying to do a downcast without knowing the derived type definition.

  class Junk {    public:      int j;      virtual void junkFunc() = 0;  };  class Base {    public:      virtual void someBaseFunction() = 0;  };  class Derived; // forward declared  Base * b;  Derived * d = (Derived *)b; // will not warn you about the reinterpret-cast; will blow up on use  Derived * d = static_cast< Derived * >( b ); // will tell you about the problem  class Derived : public Junk, public Base {    public:  };  Derived * d = (Derived *)b; // will do the right cast  Derived * d = static_cast< Derived * >( b ); // will do the right cast


C-style casts are fine for POD types, but when you're involving types that may have inheritance, you're better off with static_cast<> (which is almost always the behavior you actually want).
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement