Page 1 of 1
mRad private in Ogre::Radian
Posted: Fri Jan 28, 2011 7:33 am
by KarenRei
In Ogre::Radian, mRad is private. This makes it a pain when extending the class. Please switch it to public or protected, like the member variables of other similar classes (Vector3, Quaternion, etc).
.
Re: mRad private in Ogre::Radian
Posted: Fri Jan 28, 2011 9:01 am
by so0os
You should NEVER derive from Ogre::Radian
Re: mRad private in Ogre::Radian
Posted: Sat Jan 29, 2011 4:53 am
by Jabberwocky
so0os wrote:You should NEVER derive from Ogre::Radian
Good advice, but it would be a more helpful post if you explained it a little.

Re: mRad private in Ogre::Radian
Posted: Tue Feb 01, 2011 9:54 am
by so0os
Jabberwocky wrote:so0os wrote:You should NEVER derive from Ogre::Radian
Good advice, but it would be a more helpful post if you explained it a little.

OK.
A base class takes as many space in memory as its members, Radian has 1 float member, so it takes 4 bytes. When you derive, however, each class needs to store virtual function pointers, so you can call stuff on a base class and it gets executed for each derived child respectively. thus, size of the class grows with number of functions uhm.. methods there. It's all OK, until you do heavy math on radians (and tons of junk has to be moved in and out of stack every cycle) OR i.e. you put such struct into a VBO (that would be an overkill) or try to manually memcpy it somewhere.
Re: mRad private in Ogre::Radian
Posted: Tue Feb 01, 2011 12:48 pm
by syedhs
I really think Radian class (along with Degree) should be treated like POD (Plain Old Data) and it is - so deriving from it make it complicated unnecessarily.
Re: mRad private in Ogre::Radian
Posted: Sat Feb 05, 2011 6:23 pm
by dark_sylinc
I agree with syedhs, there should be no virtual members in the class, and the variable should change from "private" to "protected".
Not public to avoid accidental (or noobie) direct access to mRad while working with Radians.
Re: mRad private in Ogre::Radian
Posted: Sun Feb 06, 2011 1:02 am
by Kojack
When you derive, however, each class needs to store virtual function pointers, so you can call stuff on a base class and it gets executed for each derived child respectively. thus, size of the class grows with number of functions uhm.. methods there.
Inheritance on it's own doesn't add virtual function pointers. The vtable pointer is only added to an object if the class has methods which are declared as virtual.
Any number of virtual methods will cause the class to increase by only one vtable pointer, the size doesn't grow with the number of methods.
Since Radian has no virtuals, you can add as many methods to it in a derived class as you want (as long as they are non virtual too), it won't grow the object past 4 bytes.
Re: mRad private in Ogre::Radian
Posted: Sun Feb 06, 2011 12:43 pm
by madmarx
A base class takes as many space in memory as its members, Radian has 1 float member, so it takes 4 bytes.
No. Not only does the C++ standard not force it to be the same size, but it actually happens rarely.
For that reason, Visual Studio provides some pragma to force data alignment.
class A
{
};
what is sizeof(A)?
=> 1 on VS2008
class B
{
bool b1;
bool b2;
bool b3;
double b4;
};
what is sizeof(B)?
=> 16 on VS2008
Such simple tests are very easy to set up...
Best,
Pierre
EDIT : and compiler options can make this size change too.
Re: mRad private in Ogre::Radian
Posted: Mon Feb 07, 2011 3:34 am
by dark_sylinc
@madmarx: I don't think data alignment could be highly relevant on the topic, and it is very compiler option specific, and architecture specific.. The main reason for not using derived classes are virtual functions, but since there are none, there is no overhead to worry about.
Re: mRad private in Ogre::Radian
Posted: Mon Feb 07, 2011 7:47 pm
by madmarx
I don't think data alignment could be highly relevant on the topic
Yes, you are right, it is not really relevant. I just wanted to explain that a base class does not have to be the same size than its member.
Re: mRad private in Ogre::Radian
Posted: Tue Feb 08, 2011 8:54 am
by so0os
I just didn't want to elaborate too much on that.
Re: mRad private in Ogre::Radian
Posted: Tue Feb 08, 2011 1:13 pm
by CABAListic
You're getting slightly off-topic

The simple answer is that Radian is not meant or designed to be extended, and as such there is no reason to make mRad non-private.