본문 바로가기

Development/C++

(const MyClass& object) VS (MyClass& object)

If you want to pass class instances (objects), you either use

 void function(const MyClass& object){
   // do something with object  
 }

or

 void process(MyClass& object_to_be_changed){
   // change member variables  
 }

On the other hand if you want to "pass" the class itself

template<class AnyClass>
void function_taking_class(){
   // use static functions of AnyClass
   AnyClass::count_instances();
   // or create an object of AnyClass and use it
   AnyClass object;
   object.member = value;
}
// call it as 
function_taking_class<MyClass>();
// or 
function_taking_class<MyStruct>();

with

class MyClass{
  int member;
  //...
};
MyClass object1;

 

 

 

 

 

 

https://archive.ph/8P95o

https://stackoverflow.com/questions/1896369/how-to-use-a-class-object-in-c-as-a-function-parameter

 

How to use a class object in C++ as a function parameter

I am not sure how to have a function that receives a class object as a parameter. Any help? Here is an example below. #include<iostream> void function(class object); //prototype void funct...

stackoverflow.com