KVM
KVM

Reputation: 919

c++ method argument class reference

I'm sorry to ask such a newbie question. Here is my problem :

MyClass* c = new MyClass("test");
method(c);//error cannot convert MyClass* to MyClass

the definition :

method(MyClass c);

should I also define ?

method(MyClass* c);

I don't want to duplicate the code, what is the proper way ?

Upvotes: 0

Views: 93

Answers (2)

Joseph Mansfield
Joseph Mansfield

Reputation: 110738

You're clearly a Java programmer! First of all, you will need to do:

MyClass* c = new MyClass("test");

Note that c is a "pointer to MyClass" - this is necessary because the new expression gives you a pointer to the dynamically allocated object.

Now, to pass this to a function that takes a MyClass argument, you will need to dereference the pointer. That is, you will do:

method(*c);

Note that because the function takes a MyClass by value (not a reference), the object will be copied into your function. The MyClass object inside method will be a copy of the object you allocated earlier. The type of the argument depends on exactly what you want your function to do and convey. If instead you want a reference to the object, so that modifying the object inside the function will modify the c object outside the function, you need your function to take a MyClass& argument - a reference to MyClass.

If you were to have the argument be of type MyClass*, then you could simply do:

method(c);

This will give you similar semantics to passing a reference, because the pointer c will be copied into the function but the object that pointer refers to will still be the dynamically allocated MyClass object. That is, if inside the function you modify *d, the object pointed to by c is also modified.

Passing a raw pointer like this is usually not a very good approach. The function will have to explicitly check that the pointer is not null, otherwise your program may crash under certain conditions. If you want pass-by-reference semantics, use reference types - it's what they're for.

However, you're better off not dynamically allocating your MyClass object in the first place. I guess you're only using new because you did it a lot in Java. In C++, the new expression is used to dynamically allocate an object. More often than not, you do not want to dynamically allocate an object. It is perfectly fine to create it with automatic storage duration, which you would do like so:

MyClass c("test");
method(c);

It is considered very good practise in C++ to avoid pointers and dynamic allocation unless you have a good reason. It will only lead you to more complicated code with more room for errors and bugs. In fact, the code you've given already has a problem because you didn't delete c;. When you do as I have just suggested, you don't need to explicitly delete anything, because the object will be destroyed when it goes out of scope.

Upvotes: 2

hmatar
hmatar

Reputation: 2429

MyClass *c = new MyClass("test");

method(*c);

it works if you defined a method like method(MyClass c)

But if you define you method like method(MyClass *c);

then it should be

MyClass *c = new MyClass("test");
method(c);

The two alternatives have consequences depending on what you want to do with the object you have created.

Upvotes: 1

Related Questions