我试图为我的有理数字类重载+ =运算符,但我不相信它有效,因为我总是得到相同的结果:
RationalNumber RationalNumber::operator+=(const RationalNumber &rhs){
int den = denominator * rhs.denominator;
int a = numerator * rhs.denominator;
int b = rhs.numerator * denominator;
int num = a+b;
RationalNumber ratNum(num, den);
return ratNum;
}
Run Code Online (Sandbox Code Playgroud)
里面主要
//create two rational numbers
RationalNumber a(1, 3);
a.print();
RationalNumber b(6, 7);
b.print();
//test += operator
a+=(b);
a.print();
Run Code Online (Sandbox Code Playgroud)
在调用+ =(b)之后,a仍然是1/3,它应该是25/21.我有什么想法我做错了吗?
这是我的代码片段:
class Request
{
public:
Request(void);
………..
}
Request::Request(void)
{
qDebug()<<"Request: "<<"Hello World";
}
class LoginRequest :public Request
{
public:
LoginRequest(void);
LoginRequest(QDomDocument);
……………
}
LoginRequest::LoginRequest(void)
{
qDebug()<<"LoginRequest: "<<"Hello World";
requestType=LOGIN;
requestId=-1;
}
LoginRequest::LoginRequest(QDomDocument doc){
qDebug()<<"LoginRequest: "<<"Hello World with QDomDocument";
LoginRequest::LoginRequest();
xmlDoc_=doc;
}
Run Code Online (Sandbox Code Playgroud)
当调用Overrided LoginRequest的构造函数时
LoginRequest *test=new LoginRequest(doc);
Run Code Online (Sandbox Code Playgroud)
我想出了这个结果:
Request: Hello World
LoginRequest: Hello World with QDomDocument
Request: Hello World
LoginRequest: Hello World
Run Code Online (Sandbox Code Playgroud)
显然,LoginRequest的构造函数都调用了REquest构造函数.
有没有办法解决这种情况?
我可以构造另一个函数来完成我想要做的工作,并让两个构造函数都调用该函数.但我想知道有什么解决方案吗?
编辑:http: //www.parashift.com/c++-faq-lite/ctors.html#faq-10.3
我有一个String类,我想重载+来添加两个String*指针.这样的事情不起作用:
String* operator+(String* s1, String* s2);
Run Code Online (Sandbox Code Playgroud)
有没有办法避免通过引用传递.考虑这个例子:
String* s1 = new String("Hello");
String* s2 = new String("World");
String* s3 = s1 + s2;
Run Code Online (Sandbox Code Playgroud)
我需要这种额外的工作.请建议.
我有一个我认为应该工作的东西,但事实并非如此.
我有几个Controller _ ##类,它们都来自Controller_Core.每个Controller _ ## - 类都有一个公共函数Save().现在我想我要执行一些其他的一般性检查(每个$ _POST-var的addslashes)并且想如果我将一个公共函数Save()添加到Controller_Core它将默认执行,因为## - 类从它扩展.然而,这种情况并非如此.
我的问题; 我想要实现的是可能的吗?或者我误以为这会有用吗?
我发现模板方法可能会重载,我可以在模板类上做同样的事情吗?如果2个模板类与模板类实例化匹配,我们可以使用构造函数中的参数类型来推断使用哪个模板类.
template <typename T>
class A{
A(T){}
};
template <typename T>
class A{
A(T*){}
};
int main(){
A<int*> a((int*)0);
A<int> a((int*)0);
return 0;
}
Run Code Online (Sandbox Code Playgroud) g ++ 4.4.5
我有一个扩展类std :: ofstream的类来添加一些功能.
MyStream& MyStream::operator<<(const bool& val) {
if(this->pos == 8) {
this->pos = 0;
ofstream::operator<<(this->curbyte); //call the parent method
}
curbyte = curbyte + (val << pos++);
return *(this);
}
Run Code Online (Sandbox Code Playgroud)
这基本上允许你将单个位写为bool然后它将使用父<<方法写入每组8.我不得不在这里使用这个调用语法,因为我正在调用基本方法,但在我使用这个类的实际main方法中,我尝试调用以下行:
bout << (unsigned char) 255u;
Run Code Online (Sandbox Code Playgroud)
我想要调用<<方法已经为ofstream和unsigned char定义了但是它给了我一个很长的模糊的重载错误,列出了已经为ofstream定义的所有char相关的候选者(char,unsigned char,signed char)和我自己的bool方法,即使我明确地转向char.但是我确实设法让它与以下工作:
bout.operator<<((unsigned char) 255u);
Run Code Online (Sandbox Code Playgroud)
这必须与g ++如何进行隐式转换有关(我的猜测是在第一种情况下我的用户定义的转换之后还有一次可能的转换,这使得函数调用语法避免不明确).有没有人确切知道为什么会发生这种情况,或者是否有更好的语法来避免错误?
我是一个时髦的新手.也许这是小菜一碟,但我希望将数组/列表的+运算符重载为这样的代码
def a= [1,1,1]
def b= [2,2,2]
assert [3,3,3] == a + b
Run Code Online (Sandbox Code Playgroud) 在我的头文件中,我有这个:
std::string StringExtend(const std::string Source, const unsigned int Length, const bool Reverse);
std::string StringExtend(const std::string Source, const unsigned int Length);
Run Code Online (Sandbox Code Playgroud)
在我的cpp文件中,我有这个:
std::string Cranberry::StringExtend(const std::string Source, const unsigned int Length, const bool Reverse)
{
unsigned int StartIndex = (Source.length() - 1) * Reverse;
short int Increment = 1 - (Reverse * 2);
int Index = StartIndex;
std::string Result;
while (Result.length() < Length)
{
if (Reverse) Result = Source.at(Index) + Result;
else Result += Source.at(Index);
Index += Increment;
if (!InRange(Index, 0, Source.length() …Run Code Online (Sandbox Code Playgroud) 继承两个基类时,如果两者都有一个具有相同名称和签名的方法会发生什么?
class Physics
{
public:
void Update() { std::cout << "Physics!" }
};
class Graphics
{
public:
void Update() { std::cout << "Graphics!" }
};
class Shape : Physics, Graphics
{
};
int main()
{
Shape shape;
shape.Update();
}
Run Code Online (Sandbox Code Playgroud)
会发生什么?
我在这个例子中遇到了麻烦.每当我发送任何参数时,它都会给出编译错误:
prog.cpp: In function ‘int main()’:
prog.cpp:11: error: call of overloaded ‘add(std::basic_istream<char, std::char_traits<char> >&, std::basic_istream<char, std::char_traits<char> >&)’ is ambiguous
prog.cpp:4: note: candidates are: int add(int, int) <near match>
prog.cpp:6: note: char add(char, char) <near match>
Run Code Online (Sandbox Code Playgroud)
我个人认为当我将参数作为int或char发送时会发生此错误,但是当我发送浮点参数时,错误仍然存在.请帮助.谢谢
/*Function Overloading*/
#include<iostream>
using namespace std;
int add(int a,int b);
float add(float a,float b);
char add(char a,char b);
int main()
{
float a,b;
cout<<"Enter Two numbers to add them"<<'\n';
add(cin>>a,cin>>b);
return 0;
}
int add(int a,int b)
{
//cin>>a,b;
return a+b;
}
float add(float …Run Code Online (Sandbox Code Playgroud) overloading ×10
c++ ×8
function ×2
inheritance ×2
class ×1
constructor ×1
groovy ×1
iostream ×1
oop ×1
php ×1
resolution ×1
templates ×1