Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Operator overloading, passing class a automatically convert to class b


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 BaneTrapper   Members   -  Reputation: 1375

Like
0Likes
Like

Posted 23 February 2014 - 06:00 PM

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, 23 February 2014 - 06:09 PM.


Sponsor:

#2 Ectara   Crossbones+   -  Reputation: 3093

Like
0Likes
Like

Posted 23 February 2014 - 06:09 PM

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?



#3 tivolo   Members   -  Reputation: 1171

Like
0Likes
Like

Posted 23 February 2014 - 06:11 PM

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, 23 February 2014 - 06:13 PM.


#4 dejaime   Crossbones+   -  Reputation: 4135

Like
0Likes
Like

Posted 23 February 2014 - 06:25 PM

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, 23 February 2014 - 07:51 PM.


#5 NightCreature83   Crossbones+   -  Reputation: 3713

Like
2Likes
Like

Posted 24 February 2014 - 04:42 AM

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, 24 February 2014 - 04:45 AM.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#6 BaneTrapper   Members   -  Reputation: 1375

Like
0Likes
Like

Posted 25 February 2014 - 04:41 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS