jeromintus
jeromintus

Reputation: 481

return reference of static member variable c++

Is there any problem to return a reference of a static member variable? (Reference to avoid copy cost)

Vector accepts 4 parameters(x,y,z,w);

.h

class MyClass
{
private:
   static const Vector POS;
}

.cpp

const Vector MyClass::POS(100,300,0,0);

const Vector& MyClass::GetVector()
{
 return POS;
}

Upvotes: 0

Views: 3661

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409166

If POS is declared static then its lifetime is the lifetime of the program, and so returning a reference to it is safe.

Upvotes: 6

Related Questions