Operator overloading, passing class a automatically convert to class b

Started by
4 comments, last by BaneTrapper 10 years, 1 month ago

Hello.

I have a situation where i have two classes represented as "A" and "B", class A can be converted to class B, but not other way around.

pseudo code:


class A
{
public:
A(int One) : one(One);
int one;
};
 
class B
{
public:
B(int One, int Two) : one(One), two(Two);
int one, two;
};
 
void func(A & objA);...
//.cpp
A a(1);
B b(2,3);
func(b);

I require the conversion of the classes and unsure what operator to overload.

In current example the func would have a object "A" with int "one" being value 2.

I tried


class A
{
public:
A operator=(B b);
int one;
};
 
A A::operator=(B b)
{
return A(b.one);
}

and my compiler still complains that it cannot take it.

EDIT::

It seams i overlooked a simple solution it was...


class A
{
A(B b);
};

My feelings

http://www.the-mainboard.com/styles/default/xenforo/eastereggs/devine.gif

Advertisement

If I'm not mistaken, a temporary can't be passed as a reference to another function, only a const reference.

However, any solution seems a little meaningless. Isn't this a problem that would require inheritance to make any sense?

What you are probably looking for is a conversion function (or cast operator).

However, if you tell us the kind of problem you are trying to solve, maybe a solution that doesn't involve conversions can be found. I would only use a conversion function and non-explicit constructors under very special circumstances, and try to convert implicit conversions when possible.

In your case, just adding a constructor to A that takes a const-reference to B would also work (and be more safe).

Is this what you want?
#include <iostream>

class B {
	
    int x, y;
    
    public:
    B(int p_x, int p_y):
    x(p_x), y(p_y) {}
    
    int get_x() const {return x;}
    int get_y() const {return y;}
};

class A {
    int x, y, z;
    float w;
	
	public:
	int get_x() const {return x;}
	int get_y() const {return y;}
	int get_z() const {return z;}
	float get_w() const {return w;}
	
	A & operator = (B cp) {
		x = cp.get_x();
		y = cp.get_y();
		z = 0; //example
		w = 123456; //as above
		
		return *this;
	}
};

int main () {
	A a;
	B b(10, 15);
	
	a = b;
	
	std::cout<<"X: "<<a.get_x()<<std::endl;
	std::cout<<"Y: "<<a.get_y()<<std::endl;
	std::cout<<"Z: "<<a.get_z()<<std::endl;
	std::cout<<"W: "<<a.get_w()<<std::endl;
	
	return 0;
}

//Output//////////////////////////////////
//	X: 10
//	Y: 15
//	Z: 0
//	W: 123456
//////////////////////////////////////////

I never could resist creating code snippets...

If I'm not mistaken, a temporary can't be passed as a reference to another function, only a const reference.

However, any solution seems a little meaningless. Isn't this a problem that would require inheritance to make any sense?

Even with a const reference that will not work, because the temporary will scope out at the function call. I have been bitten by this stuff before and its not an easy bug to detect.

To transform the object you will want to overload the constructor, to take in a B and construct an A from it. I don't recommend this though, implicit conversions lead to weird errors later on, so if you do this mark the constructor with "explicit". This makes it so that you explicitly have to construct on object on the function line, and pass by value instead of reference to make it work correctly.

In code the explict does this:


class Test
{
public:
    explicit Test(int x) : m_value(x) {}
 
    int m_value;
}
 
void foo(Test test) { std::cout << test.m_value << std::endl; }
 
void main()
{
    int x = 5
    foo(x); //Compile error due to explicit on the constructor, not having explicit would allow an implicit conversion here.
    foo(Test(x)); //Works fine now.
}

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

I apologize i forgot i made the post.

What i needed is from two clases "A" and "B" to construct a new instance object of class "A" from data of object "B".

I over thought/looked the solution, simple constructor was the solution.

This topic is closed to new replies.

Advertisement