Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualHodgman

Posted 02 March 2013 - 09:08 PM

Engine creates an instance of the renderer and at the same time passes a this pointer to the constructor of the renderer class which then stores this reference as a local member to have access to the engine and its components.
So basically often it is the case that if a certain class needs something from the timer class, it accesses it by using its local pointer to the engine class, which then gets its timer component.

If the renderer needs a timer, then just pass it a timer! Don't pass it some other object that is able to provide a timer indirectly.
 
 

In my application I create my components as pointer to an object. So during runtime these pointers are getting passed around as e.g. Core* core or Renderer* renderer as parameters of the functions. As I understand this only passes a copy of the pointer to this specific object to the other class so it is rather quick, right ? Now when returning e.g. floats would I then return the address to this float member to make it as quick as possible ? Also what is the best way to pass an std::vector?

Big, complex objects (engine classes, std::vector, etc), which are expensive to copy (or can't be copied), IMO you should prefer passing by const-reference (but const-pointer is also valid).
For primitive types, like float/int/etc, you should always pass them by value.
The exception to these rules is when you need an "out argument" -- objects that will be modified by a function -- in which case you should pass by non-const pointer.
e.g.
float CalculateSum( const std::vector<float>& input );
void PushToVector( int input, std::vector<int>* output );
Foo* GetFooComponent();
const Foo& GetFooComponent() const;
Note that the use of pointers vs references is just a style choice!
But, using values vs pointers/references is a performance issue for large objects.

#1Hodgman

Posted 02 March 2013 - 09:05 PM

Engine creates an instance of the renderer and at the same time passes a this pointer to the constructor of the renderer class which then stores this reference as a local member to have access to the engine and its components.
So basically often it is the case that if a certain class needs something from the timer class, it accesses it by using its local pointer to the engine class, which then gets its timer component.

If the renderer needs a timer, then just pass it a timer! Don't pass it some other object that is able to provide a timer indirectly.

 

In my application I create my components as pointer to an object. So during runtime these pointers are getting passed around as e.g. Core* core or Renderer* renderer as parameters of the functions. As I understand this only passes a copy of the pointer to this specific object to the other class so it is rather quick, right ? Now when returning e.g. floats would I then return the address to this float member to make it as quick as possible ? Also what is the best way to pass an std::vector?

Big, complex objects (engine classes, std::vector, etc), which are expensive to copy (or can't be copied), IMO you should prefer passing by const-reference (but const-pointer is also valid).

For primitive types, like float/int/etc, you should always pass them by value.

The exception to these rules is when you need an "out argument" -- objects that will be modified by a function -- in which case you should pass by non-const pointer.

e.g.

float CalculateSum( const std::vector<float>& input );
void PushToVector( int input, std::vector<int>* output );

PARTNERS