考虑一下代码:
#include <stdio.h>
class Base {
public:
virtual void gogo(int a){
printf(" Base :: gogo (int) \n");
};
virtual void gogo(int* a){
printf(" Base :: gogo (int*) \n");
};
};
class Derived : public Base{
public:
virtual void gogo(int* a){
printf(" Derived :: gogo (int*) \n");
};
};
int main(){
Derived obj;
obj.gogo(7);
}
Run Code Online (Sandbox Code Playgroud)
得到此错误:
>g++ -pedantic -Os test.cpp -o test test.cpp: In function `int main()': test.cpp:31: error: no matching function for call to `Derived::gogo(int)' test.cpp:21: note: candidates are: virtual …
我写下面的代码是为了解释我的问题.如果我注释第11行(使用关键字"using"),编译器不会编译该文件并显示以下错误:invalid conversion from 'char' to 'const char*'.它似乎没有void action(char)在Parent类中看到Son类的方法.
为什么编译器会以这种方式运行?或者我做错了什么?
class Parent
{
public:
virtual void action( const char how ){ this->action( &how ); }
virtual void action( const char * how ) = 0;
};
class Son : public Parent
{
public:
using Parent::action; // Why should i write this line?
void action( const char * how ){ printf( "Action: %c\n", *how ); }
};
int main( int argc, char** argv ) …Run Code Online (Sandbox Code Playgroud) 鉴于以下示例,为什么我必须明确使用该语句b->A::DoSomething()而不仅仅是b->DoSomething()?
编译器的重载决议不应该弄清楚我在谈论哪种方法?
我正在使用Microsoft VS 2005.(注意:在这种情况下使用虚拟无效.)
class A
{
public:
int DoSomething() {return 0;};
};
class B : public A
{
public:
int DoSomething(int x) {return 1;};
};
int main()
{
B* b = new B();
b->A::DoSomething(); //Why this?
//b->DoSomething(); //Why not this? (Gives compiler error.)
delete b;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在下面的例子中,我试图通过在课堂上将其隐藏起来来隐藏using Employee::showEveryDept最后一个子Designer类Elayer-
#include <iostream>
class Employee {
private:
char name[5] = "abcd";
void allDept() { std::cout << "Woo"; }
public:
void tellName() { std::cout << name << "\n"; }
virtual void showEveryDept()
{
std::cout << "Employee can see every dept\n";
allDept();
}
};
class ELayer : public Employee {
private:
using Employee::showEveryDept;
protected:
ELayer() {}
public:
using Employee::tellName;
};
class Designer : public ELayer {
private:
char color = 'r';
public:
void showOwnDept() { …Run Code Online (Sandbox Code Playgroud) 假设我有一些像这样的代码:
class Base {
public:
virtual int Foo(int) = 0;
};
class Derived : public Base {
public:
int Foo(int);
virtual double Foo(double) = 0;
};
class Concrete : public Derived {
public:
double Foo(double);
};
Run Code Online (Sandbox Code Playgroud)
如果我有一个Concrete类型的对象,为什么我不能调用Foo(int)?
如果我更改Foo的名称(double)以便它不会重载Foo,那么一切都很好并且两种方法都可以访问,但这不是我想要的.
类似地,如果我在Derived中删除Concrete类并实现Foo(double),那么两者都是可访问的,但同样,不是我想要的.
我需要理解为什么C++不允许在Child中访问Grandparent重载函数,如果在Parent中声明了任何重载函数.请考虑以下示例:
class grandparent{
public:
void foo();
void foo(int);
void test();
};
class parent : public grandparent{
public:
void foo();
};
class child : public parent{
public:
child(){
//foo(1); //not accessible
test(); //accessible
}
};
Run Code Online (Sandbox Code Playgroud)
这里,两个函数foo()和foo(int)是Grandparent中的重载函数.但是foo(int)是不可访问的,因为foo()在Parent中声明(如果声明它是public或private或protected,则无关紧要).但是,test()是可访问的,根据OOP是正确的.
我需要知道这种行为的原因.
我在C++中面临一个问题:
#include <iostream>
class A
{
protected:
void some_func(const unsigned int& param1)
{
std::cout << "A::some_func(" << param1 << ")" << std::endl;
}
public:
virtual ~A() {}
virtual void some_func(const unsigned int& param1, const char*)
{
some_func(param1);
}
};
class B : public A
{
public:
virtual ~B() {}
virtual void some_func(const unsigned int& param1, const char*)
{
some_func(param1);
}
};
int main(int, char**)
{
A* t = new B();
t->some_func(21, "some char*");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用g ++ 4.0.1和编译错误:
$ …Run Code Online (Sandbox Code Playgroud) 可能重复:
在派生类中具有相同名称但签名不同的函数
我正在尝试编译这个,我无法弄清楚代码有什么问题.我正在使用MacOSX Snow Leopard和Xcode g ++版本4.2.1.有人能告诉我这是什么问题吗?我认为这应该编译.这不是我的作业,我是一名开发人员......至少我以为自己被这个问题困住了.我收到以下错误消息:
error: no matching function for call to ‘Child::func(std::string&)’
note: candidates are: virtual void Child::func()
Run Code Online (Sandbox Code Playgroud)
这是代码:
#include <string>
using namespace std;
class Parent
{
public:
Parent(){}
virtual ~Parent(){}
void set(string s){this->str = s;}
virtual void func(){cout << "Parent::func(" << this->str << ")" << endl;}
virtual void func(string& s){this->str = s; this->func();}
protected:
string str;
};
class Child : public Parent
{
public:
Child():Parent(){}
virtual ~Child(){}
virtual void func(){cout << "Child::func(" << this->str << ")" …Run Code Online (Sandbox Code Playgroud) 我不明白这段代码有什么问题.它看起来像一个不可思议的陷阱!
这段代码:
class Foo
{
public:
virtual double foo(double x) const = 0;
double foo(int x) const { return (double)(x + x); }
};
class Bar : public Foo
{
public:
virtual double foo(double x) const { return x * x; }
};
int main()
{
Bar* b = new Bar;
Foo* f = b;
std::cout << b->foo(3) << " " << f->foo(3) << std::endl;
std::cout << b->foo(5.0) << " " << f->foo(5.0) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
打印以下输出:
9 …Run Code Online (Sandbox Code Playgroud) c++ ×9
inheritance ×4
overloading ×4
overriding ×3
c++11 ×2
oop ×2
polymorphism ×2
virtual ×2
final ×1
function ×1
g++ ×1
resolution ×1
using ×1