相关疑难解决方法(0)

为什么派生类中的重写函数会隐藏基类的其他重载?

考虑一下代码:

#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 …

c++ polymorphism overriding

212
推荐指数
3
解决办法
5万
查看次数

为什么我应该使用"using"关键字来访问我的基类方法?

我写下面的代码是为了解释我的问题.如果我注释第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)

c++ oop inheritance using

66
推荐指数
4
解决办法
4万
查看次数

C++重载决议

鉴于以下示例,为什么我必须明确使用该语句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)

c++ overloading resolution function

36
推荐指数
4
解决办法
1万
查看次数

using-declaration不能正常工作

在下面的例子中,我试图通过在课堂上将其隐藏起来来隐藏using Employee::showEveryDept最后一个子DesignerElayer-

#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)

c++ polymorphism inheritance using-declaration c++11

12
推荐指数
3
解决办法
915
查看次数

在C++中重载子类中的方法

假设我有一些像这样的代码:

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++ inheritance overloading

10
推荐指数
2
解决办法
5651
查看次数

祖父母在孩子身上重载了功能

我需要理解为什么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++ overriding overloading

9
推荐指数
1
解决办法
1477
查看次数

C++覆盖/过载问题

我在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)

c++ inheritance overriding overloading g++

8
推荐指数
1
解决办法
2622
查看次数

简单的C++继承示例,出了什么问题?

可能重复:
在派生类中具有相同名称但签名不同的函数

我正在尝试编译这个,我无法弄清楚代码有什么问题.我正在使用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)

c++ oop

5
推荐指数
1
解决办法
2451
查看次数

C++ 0x中的最终虚函数

读到你可以在C++ 0x中拥有最终的虚函数我有点困惑.首先省略两个修饰符有什么区别?

virtual final c++11

5
推荐指数
1
解决办法
308
查看次数

调用虚函数时的奇怪行为

我不明白这段代码有什么问题.它看起来像一个不可思议的陷阱!

这段代码:

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++ virtual

5
推荐指数
1
解决办法
135
查看次数