Jump to content
  • Advertisement
Sign in to follow this  
BaneTrapper

Operator overloading, passing class a automatically convert to class b

This topic is 2057 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Edited by BaneTrapper

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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).

Edited by tivolo

Share this post


Link to post
Share on other sites
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... Edited by dejaime

Share this post


Link to post
Share on other sites

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.
}
Edited by NightCreature83

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!