🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Class functions

Started by
6 comments, last by Yanroy 24 years, 5 months ago
My last post about this was thouroughly buried, so I decided to reiterate some stuff here... This is a little different anyway. If I make a class that has a function in it. class MyClass { void MyFunc() {;} }; and then create 10 instances of that class MyClass Temp[10]; Will the compiler make 10 differnt MyFunc()''s? One for each instance, and waste memory, or will it just make one to use with all of them? www.trak.to/rdp

Yanroy@usa.com

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Advertisement
I think it uses the same function each time but it makes variable spaces for each instance (formal and static variables in the function), but I''m not sure
GunnerSteinn got it right !! The functions itself do just exist one time in memory, but the member variables do exist for every instance. So, OOP languages aren''t correctely OOP in memory at run-time (because the functions don''t exist for every instance as you can see in the source-code), the same goes for Java, too !!

Phillip
The data members are stored just like in struct. When you call a member function, the compiler set the this pointer to the current classes data members. Eventhough you don''t actually see it, when you use a member variable in a member function you may right a = 5, but the compiler translates it to this->a = 5

You can use the this pointer in your member functions, and it can be very useful at times.

Domini
I knew that there was only one member function used by the class, however, the question of static variables inside member functions was not really clarified.
If you have a static variable inside a member function of a class, does every call to that function from any instantiation of the class use the same initialised variable or is there one static variable per class instance?

Cheers,

Mike
There are seperate versions of static variables in every instance of the class.
No, static variables only exist once per class, not once per class instance. Otherwise what would be the point behind using static variables? Static function variables also exist only once per function. Static variables also initialize at load time. Static variables also exist in the data segment of a program, rather than the stack or heap segments.

When the compiler sees something like

void MyClass::MyFunc(int arg1, int arg2) {
}

it translates it into something like

void _MyClass_!$##$_MyFunc(MyClass * this, int arg1, int arg2) {
}

Yes the !$##$ is something the compiler does, and not obscene words. Look at a symbol dump from a C++ obj file.

You can see then why it doesn''t create multiple copies of the function. There''s only one function that accepts an arg of the class type.

This is a correct implementation for OOP functions, as proper object oriented code does have dynamic code transformations on a per-object basis. If it did, that would be a subversion of the polymorphism and virtual function systems.
Doh! Sorry

I think this is appropriate:
"If the theory doesn''t fit the facts, change the facts"
- Einstein

This topic is closed to new replies.

Advertisement