Returning object of ClassAAA in function from ClassBBB

Is it possible to do something along the lines of,

1
2
3
4
5
6
7
8
const ClassBBB& operator*(const ClassAAA& first, const ClassBBB& second) {
     if (condition) {
          return first;
     }
     else {
          return second;
     }
}


If possible, how do I return first ? What do I need to code/implement, and in which source file, (ClassAAA or ClassBBB)?
Last edited on
That depends on the relationship between ClassAAA and ClassBBB. The snippet you posted only works if ClassAAA inherits from ClassBBB (which you haven't indicated).

Or if both ClassAAA and ClassBBB inherit from a common base class, you can do this:
1
2
3
const ClassBase & operator * (const ClassAAA & first, const ClassBBB & second) 
{   return (condition) ? first : second; 
}

Last edited on
Hello,

The issue Im having is that ClassBBB is derived from ClassAAA, but it can't return ClassAAA objects passed as paramters.
1
2
3
4
5
6
7
8
9
	    const ClassBBB& operator*(const ClassAAA& first, const ClassBBB& second) {
	        //do stuff
	        if (condition) {
	            return second;
	        }
	        else {
	            return first;
	        }
	    } <!--//error, no suitable conversion from ClassAAA to ClassBBB 

I've tried a workaround. Within this function I created local instances of both objects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	ClassAAA::ClassAAA(const char* str, const int hp, const int at) {
		if (condition) {
                     //do stuff
		}
		else {
			setEmpty();
		}
	}

	ClassBBB::ClassBBB(const char* sn, const int hp, const int at, const int bs, const int def) : ClassAAA(sn, hp, at) {
		if (condition) {
			ClassAAA(sn, hp, at);
			bonus = bs;
			defense = def;
		}
		else {
			ClassBBB::setEmpty();
		}
	}


And so therefore when const ClassBBB& operator*(const ClassAAA& first, const ClassBBB& second) gets called, I run into an issue, because it's telling me this function can't return ClassAAA object reference.

I've even tried creating pointers to those objects passed as parameters, but it didnt work. Is that something I should be focusing on, pointers, or am I simply just missing something elementary?
Last edited on
am I simply just missing something elementary?

Yes, you're missing the fact that you can't return a class that something is not derived from.
i.e. If B is derived from A, you can return an A, but the return type of your operator must be A.
If B is derived from A and the return type is A, you can return B, but B will be "downcast" to A.

See my ClassBase example in my previous post for a way to do this.

I've even tried creating pointers to those objects passed as parameters, but it didnt work.

Pointers won't change anything, other than to add a level of indirection. The return type must match, or be a base class of both.
Last edited on
Ah, I understand now.

Basically what you're saying is to declare

const ClassAAA& operator*(const ClassAAA& first, const ClassBBB& second)

in ClassBBB, and define it accordingly in ClassBBB as well.

Thank you for your input
Topic archived. No new replies allowed.