user3348712
user3348712

Reputation: 161

c++ using a different class for an argument?

I have a simple question, when I'm writing the .h file of a class and want to pass an argument of type of a different class how should it be written?

For example:

#include "y.h"
class x
{
public :
void method( y &)
};

In void method, is that right? Or should it be written as y::y&? And when it's implemented in a .cpp file?

Upvotes: 0

Views: 71

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

If do not take into account qualifier volatile then you have the following possibilies

void method( y &);
void method( const y &);
void method( y );
void method( const y * );
void method( y * );

Declaration

void method( const y );

declares the same function as

void method( y );

Also the method itself can have qualifier const. For example

void method( y &) const;

Also if the class name will be hidden then you can use the elaborated name. For example

void method( class y &) const;

This declaration

void method( y::y &);

is correct provided that the left y is the name of a namespace and the right y is the name of a class defined in the namespace.

Upvotes: 0

Alexander Jones
Alexander Jones

Reputation: 11

If your class is called y then what you have written is correct. The :: syntax is for referencing names within namespaces or other classes. In this particular case, y::y would refer to the constructor of y, not the class itself.

There is no change to this within the implementation (.cpp) file - the name y references the same class in both cases.

Upvotes: 1

Related Questions