#include <stdio.h>
int Add(int a, int b);
int Add(int a, int b, int c);
double Add(double a, double b);
void main()
{
printf("1+2=%d\n",Add(1,2));
printf("3+4+5=%d\n",Add(3,4,5));
printf("1.414+2.54=%f\n",Add(1.414,2.54));
}
int Add(int a, int b)
{
return a+b;
}
int Add(int a, int b, int c)
{
return a+b+c;
}
double Add(double a, double b)
{
return a+b;
}
Run Code Online (Sandbox Code Playgroud)
我用C语言编写并使用Xcode.在研究"重载"时,Xcode会显示错误消息,指出无法执行过载.相反,它显示"添加'的冲突类型"消息.使用Xcode,过载是不是可以工作?
我对C++重载分辨率的行为感到困惑.我有两个班,A和B,A <:B.A具有虚函数f,B应该覆盖该函数.
但是,当我使用引用调用虚函数时,虚函数似乎不像虚函数那样工作.起初我认为这是由于对象被分配在堆栈而不是堆上,但现在我发现即使我使用对堆上分配的对象的引用也会发生这种奇怪的行为.
对此有何解释?
#include<iostream>
using namespace std;
class A{
public:
virtual void foo(){ cout << "A::foo" << endl; }
};
class B : public A{
public:
virtual void foo(){ cout << "B::foo" << endl; }
};
void test_pt(A* pt){
cout << "test_pt " << (int)pt << " ";
pt->foo();
}
void test_ref(A ref){
cout << "test_ref " << (int)&ref << " ";
ref.foo();
}
int main(int argc, char* argv[]){
// pointers to objects allocated on heap
A* heap_pt_a = …Run Code Online (Sandbox Code Playgroud) c++ virtual-functions overloading reference overload-resolution
有人可以告诉我为什么我收到这个"没有可行的超载"?我很困惑为什么我收到这个....我是一个菜鸟.
int main()
{
char ch;
vector<int> temp;
ifstream infile;
infile.open("tempsF.txt");
if (infile.fail())
{
cout << "Could not open file numbers." << "\n";
return 1;
}
int data;
infile >> data;
while (!infile.eof())
{
if(isalpha(ch) || ispunct(ch))
{
if(isupper(ch) && ch != '\n')
temp += " ";<<<<<<<<<<<<<<<<<<<<<<<<< No Viable Overloaded '+='
temp += ch;<<<<<<<<<<<<<<<<<<<<<<<<<< No Viable Overloaded '+='
}
}
Run Code Online (Sandbox Code Playgroud) 这是我的代码:
public class XYZ {
public static void overLoaded(Object a) {
System.out.println("IN OBJECT");
}
public static void main(String[] args) {
overLoaded();
}
}
Run Code Online (Sandbox Code Playgroud)
我期望编译时错误,因为没有arg方法,而是输出结果是IN OBJECT.
请解释输出.
我有3节课
class A {
//...
}
class B : public A {
//...
}
Run Code Online (Sandbox Code Playgroud)
和C类:
#include "A.h"
#include "B.h"
class C
{
void method(A anObjOfA);
void method(B anObjOfB);
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我这样做
B* ptr = new B();
A* ptrToB = ptr;
c.method(*ptrToB);
Run Code Online (Sandbox Code Playgroud)
它调用A类型的对象的方法,而不是继承的实际类型B.我怎样才能确保调用inheritence-tree中最深的对象的正确函数,而不是在编译时实际知道它的类型?
PS:我确定这是一个noob问题,对于我的生活,我在这里找不到任何结果,因为每个人都在忙着理解"虚拟"关键字,这对我来说非常清楚,但不是问题这里.
在一项任务中,我被告知要bool operator()(const T&, const T&)为某些课程实施.重载bool operator意味着允许隐式转换对象bool.用两个参数重载它是什么意思?这些参数如何在主叫方面传递?这是怎么用的?
我有A级,
class A{
private:
int num;
public:
A(int n){ num = n; };
int getNum(){
return num;
}
A operator+(const A &other){
int newNum = num + other.getNum();
return A(newNum);
};
};
Run Code Online (Sandbox Code Playgroud)
为什么会other.getNum()出错?我可以other.num很好地访问other()中的变量,但似乎我不能使用任何其他函数.
我得到的错误就是这样的
参数无效:候选者是int getNum().
我可以写int test = getNum()但不是int test = other.getNum(),但我几乎可以肯定我能以other.getNum()某种方式打电话.
我忽略了什么吗?
我有一个继承自unordered_map的自定义类,如下所示:
class _map : public unordered_map<string, _pointer> {
public:
// STUFF ...
};
Run Code Online (Sandbox Code Playgroud)
让我们假设这_pointer是另一个自定义类,它的作用是帮助我处理指针的东西._pointers可以从许多不同的预定义类型构造,例如:
vector<...> v;
_pointer p = _pointer(v);
// p is now a _pointer holding v and vector<...> data
Run Code Online (Sandbox Code Playgroud)
现在,如果我想_pointer在我的地图上存储vector <...> ,我可以简单地做:
_map map;
vector<...> v;
map["a_vector"] = _pointer(v);
Run Code Online (Sandbox Code Playgroud)
它有效,但我希望能够做到这一点:
_map map;
vector<...> v;
map["a_vector"] = v;
Run Code Online (Sandbox Code Playgroud)
并将它_pointer(v)自动分配,也许通过重载map如何分配东西并明确告诉它vector<...>通过调用特定的_pointer构造函数来处理它.
我试图这样做(通过重载operator [],operator =)但似乎我不会去某个地方.任何人都可以建议我实现这种行为的方法吗?
谢谢.
常问问题:
问:为什么在标识符名称上使用_?难道你不知道这是不好的做法吗?
答:手头的问题无关紧要.问:您的代码不是RAII.
A:那么,什么?问:你为什么不使用unique_ptr/smart_ptr?你为什么直接处理指针?
答:因为我需要,而且对于手头的问题也无关紧要.问:你为什么要重载unordered_map?你为什么不用这个_OTHER_OPTION_?
答:不,谢谢,这是我需要做的.
从书中读取重载的构造函数后,我厌倦了以下代码:
public class Employee {
String name;
int idNumber;
public Employee(){
this("JJ", 0);
System.out.println(name +" "+ idNumber);
}
public Employee(String name, int num){
this.name = name;
idNumber = num;
System.out.println(name +" 2nd "+ idNumber);
}
}
public class Object1 {
public static void main(String[] args) {
Employee emp = new Employee();
}
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
JJ 2nd 0
JJ 0
Run Code Online (Sandbox Code Playgroud)
我真的很困惑.为什么"JJ 2nd 0"首先打印出"JJ 0"?我创建了一个雇员对象emp并且没有在参数中传递任何args,是不是先调用第一个构造函数?
Mooing Duck 在这里发表评论"一个函数不能返回多个类型.但是,你可以专门化或委托重载,这很好."
我开始考虑这个问题,我想弄清楚,这个法律代码是怎样的:
template <typename T>
T initialize(){ return T(13); }
Run Code Online (Sandbox Code Playgroud)
通话时:
auto foo = initialize<int>();
auto bar = initialize<float>();
Run Code Online (Sandbox Code Playgroud)
这不会转换为仅由return-type重载的2个同名函数吗?
overloading ×10
c++ ×7
java ×2
c ×1
casting ×1
class ×1
constructor ×1
inheritance ×1
oop ×1
reference ×1
return-type ×1
templates ×1
vector ×1
xcode ×1