简体   繁体   中英

object instantiation in c++

In C++ is it true that if you instantiate an object of a class, that for every object all of methods of the class are copied for the new object?

I tried to point to a method of a class with two different objects, but I'd problems with pointer to member.

Any idea?

In C++ is it true that if you instantiate an object of a class, that for every object all of methods of the class are copied for the new object?

No, member functions are not usually copied anywhere. A different implicit parameter this is instead passed to any non-static member function, for each object of that class-type.

No, that is absolutely not true.

Class instances (objects) contain data members . Function members look like they're "in" the class, but that's only for scoping and such: your function code doesn't "exist" inside the type, and it certainly doesn't exist inside the object .


I think it could, theoretically , in that the standard doesn't outright forbid it. But honestly, no. Just no.

The code for a class exists only once.

For getting a pointer to a member function (probably what you meant by method), take a look at std::function , and for attaching the function call to different objects, take a look at std::bind .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM