does creating a c++ class that contains methods create new instances of those methods?

Denver Cox 21 Reputation points
2021-07-07T18:55:07.35+00:00

Does creating a new instance of a class make new instances of every method within the class?
class example{ public: int x; void dosomething(){ x++; } };
would this make multiple instances of the 'dosomthing' function everytime a new instance of the class is created, or would it act like the code below.
class example{ int x; } void dosomething_not_in_class(example* e){ e->x++; }

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,504 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sam of Simple Samples 5,516 Reputation points
    2021-07-07T19:37:31.837+00:00

    No, there would not be multiple instances. There would be only one copy of the method shared by all instances. Each instance gets its own stack for storing data local to that instance. Search for articles describing thread-safety and reentrancy; those topics are related to each other and to your question.

    Related to that, for DLLs there is only one copy of a DLL in physical memory that is shared by all processes (applications also called address spaces) in the system using virtual storage.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful