user140053
user140053

Reputation:

C++ pointer and reference

In my C++ class I need to assign a method with pointer and/or reference. So I do this tricky thing :

(Assuming aclass is a class variable AnotherClass *)

void MyClass::setElem(AnotherClass *pVal)
{
  aclass = pVal;
}

void MyClass::setElem(AnotherClass &refVal)
{
  aClass = &article;
}

But in my opinion, sounds not so "graceful"...

Better way to achieve this ?

Upvotes: 0

Views: 111

Answers (1)

user93353
user93353

Reputation: 14039

void MyClass::setElem(AnotherClass *pVal)
{
  aclass = pVal;
}

void MyClass::setElem(AnotherClass &refVal)
{
  setElem(&refVal);
}

Is this graceful enough? As Mr. Pitt once said in Seinfeld "Well, you don't want too much grace or you won't be able to stand".

Upvotes: 7

Related Questions