我试图将一种类类型的数据转换为另一种类类型,因此我在C++的oops程序中使用了强制转换运算符方法。
class example2;
class example1
{
int id;
int numbers;
int cost;
public:
example1(int a, int b, int c)
{
id=a;
numbers=b;
cost=c;
}
// getid() added here
// getnumber() added here
// getcost() added here
operator example2()
{
example2 temp;
temp.id=id;
temp.value=numbers*cost;
return temp;
}
};
class example2
{
int id;
int value;
public:
example2(int x,int y){
id=x;
value=y;
}
void display(){
cout<<"id "<<id<<endl;
cout<<"value "<<value<<endl;
}
};
当我使用从example1到example2的转换时。显示错误。
int main()
{
example1 s1(100,5,140.0);
example2 s2;
s2=s1;
s2.display();
return 0;
}
它给出了错误,但是为什么呢?我在example1类中创建了一个运算符重载成员函数,因为必须将example1的对象更改为example2的对象,因此该函数仅从我认为的类example1的方法中调用,应该是正确的
错误是这样的:
错误:返回类型'class example2'不完整且
example2 temp;输入的类型不完整
我以某种方式解决了这个问题,我在example2类中添加了一个构造函数:
example2(example1 e)
{
id=e.getid(); //these functions already added in my code i didnt mentioned them in here.
value=e.getnumber()*e.getcost();
}
并在example1中注释“operator example2()”部分。
现在它正在工作。
但是以前的方式是不接受。请帮助我以以前的方式来纠正我。
最佳答案
其中一个类必须首先定义,第二个类直到定义后才能完全使用。这意味着您必须对类定义进行一些分解。
这是一个基于OP帖子的如何进入尚未定义的类的示例。解释以嵌入在代码中的注释的形式出现,以将代码全部保留在一个可剪切,可粘贴的块中。
#include <iostream>
class example2; // forward declaration to satisfy compiler until example2
// is defined
class example1
{
int id;
int numbers;
int cost;
public:
example1(int a, int b, int c)
{
id = a;
numbers = b;
cost = c;
}
example1(const example2 & e) // the const and reference are just because
// no point copying e, and const ensures no
// side effects to e
{
*this = e; // why duplicate code? Just calling operator=
}
example1& operator=(const example2 & e);
// note the lack of an implementation. This is because at this point
// the compiler only knows example2 exists, but not what it looks
// like. Can't copy what what you haven't seen.
int getid() const //const to allow me to use const references.
{
return id;
}
int getnumber() const
{
return numbers;
}
int getcost() const
{
return cost;
}
void display()
{
std::cout << "Example 1" << std::endl;
std::cout << "id " << id << std::endl;
std::cout << "numbers " << numbers << std::endl;
std::cout << "cost " << cost << std::endl;
}
};
class example2
{
int id;
int value;
public:
example2(int x, int y)
{
id = x;
value = y;
}
example2(const example1 &e)
{
*this = e; // once again just calls the equals operator
}
example2 & operator=(const example1 & e) // OK. This time we know what
// example1 looks like and can
// actually implement the method
{
id = e.getid();
value = e.getnumber() * e.getcost();
return *this;
}
int getid() const
{
return id;
}
int getvalue()const
{
return value;
}
void display()
{
std::cout << "Example 2" << std::endl;
std::cout << "id " << id << std::endl;
std::cout << "value " << value << std::endl;
}
};
// and now for the implementation of example1's equals operator
example1& example1::operator=(const example2 & e)
{
id = e.getid();
numbers = -1; //do real work to get cost and numbers from e.getvalue()
cost = -1;
return *this;
}
int main()
{
example1 s1(100, 5, 140.0);
example2 s2(1, 2);
s2 = s1;
s2.display();
example2 s3(314, 278);
s1 = s3;
s1.display();
return 0;
}
现在用于重载强制转换运算符。这几乎是您永远都不想做的事情,因为几乎总是有更明显的方法来实现相同的目标。
例如:
#include<iostream>
class Integer
{
public:
Integer(int val):mVal(val)
{
}
operator int()
{
return mVal;
}
int getVal()
{
return mVal;
}
private:
int mVal;
};
int main()
{
Integer t(42);
int x = (int)t; // We're turning the Integer into an int? Kinda makes sense.
std::cout << x << std::endl;
x = t.getVal(); // hey! we're getting the integer's value! Do I need this comment?
std::cout << x << std::endl;
}