相关疑难解决方法(0)

虚函数问题

我有两节课:

class x {
public:
  virtual void hello() {
    std::cout << "x" << std::endl;
  }
};

class y : public x {
public:
  void hello() {
    std::cout << "y" << std::endl;
  }
};
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么以下两个调用hello()打印不同的消息?他们为什么不打印"y"?是因为第一个是副本而第二个实际指向内存中的对象?

int main() {
  y  a;

  x b = a;
  b.hello(); // prints x

  x* c = &a;
  c->hello(); // prints y
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ virtual-functions

7
推荐指数
2
解决办法
279
查看次数

如何有条件地实例化不同的子类?

例如,在main函数中,我想获取用户的输入.根据输入,我将创建a Rectangle或a Circle,它们是子类Object.如果没有输入(或未知),那么我将只创建一个通用对象.

class Object
{ 
       public:
           Object();
           void Draw();
       private:
           ....  
};
class Rectangle:public Object
{ 
       public:
           Rectangle();
           .... //it might have some additional functions
       private:
           ....  
};

class Circle:public Object
{ 
       public:
           Circle();
           .... //it might have some additional functions
       private:
           ....  
};
Run Code Online (Sandbox Code Playgroud)

主功能:

string objType;
getline(cin, objType);

if (!objType.compare("Rectangle"))
     Rectangle obj;
else if (!objType.compare("Circle"))
     Circle obj;
else 
     Object obj;

obj.Draw();
Run Code Online (Sandbox Code Playgroud)

当然,上面的代码不起作用,因为我不能在If语句中实例化一个对象.所以我试过这样的事情.

Object obj;
if (!objType.compare("Rectangle"))
    obj = Rectangle();
else if (!objType.compare("Circle"))
    obj = …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance class

7
推荐指数
1
解决办法
3075
查看次数

c ++:can vector <Base>包含Derived类型的对象吗?

标题基本概括了所有内容.基本上,这样做是合法的:

class Base {
    //stuff
}

class Derived: public Base {
    //more stuff
}

vector<Base> foo;
Derived bar;
foo.push_back(bar);
Run Code Online (Sandbox Code Playgroud)

基于我见过的其他帖子,以下是可以的,但我不想在这种情况下使用指针,因为它更难以使其线程安全.

vector<Base*> foo;
Derived* bar = new Derived;
foo.push_back(bar);
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism vector object-slicing

7
推荐指数
1
解决办法
4059
查看次数

通过对基类的引用来调用虚函数

当我运行它时,此代码拒绝将适当的消息打印到控制台.使用指针而不是引用似乎工作( - >而不是.).我是OOP的新手,请原谅我,如果你觉得这太荒谬了.

#include <iostream>

using namespace std;

class instrument {
public:
    virtual void play(){}
};

class drum : public instrument {
public:
    void play(){
        cout << "dum, dum" << endl;
    }
};

class piano : public instrument {
public:
    void play(){
        cout << "pling" << endl;
    }
};

int main (){
    instrument i;
    piano p;
    drum d;

    instrument &pi = i;
    pi.play();  // -

    pi = p;
    pi.play();  // pling

    pi = d;
    pi.play();  // dum, dum
}
Run Code Online (Sandbox Code Playgroud)

c++ inheritance reference

7
推荐指数
1
解决办法
9766
查看次数

成员变量多态性和参考参数

我是C++的新手,对成员变量多态性有疑问.我有以下类定义 -

class Car
{
    public:
        Car();
        virtual int getNumberOfDoors() { return 4; }
};

class ThreeDoorCar : public Car
{
    public:
        ThreeDoorCar();
        int getNumberOfDoors() { return 3; }
};

class CarPrinter
{
    public:
        CarPrinter(const Car& car);
        void printNumberOfDoors();

    protected:
        Car car_;
};
Run Code Online (Sandbox Code Playgroud)

和实施

#include "Car.h"

Car::Car()
{}

ThreeDoorCar::ThreeDoorCar()
{}

CarPrinter::CarPrinter(const Car& car)
: car_(car)
{}

void CarPrinter::printNumberOfDoors()
{
    std::cout << car_.getNumberOfDoors() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

问题是当我运行以下命令时,会调用父类的getNumberOfDoors.我可以通过使成员变量Car成为指针来解决这个问题,但我更喜欢通过引用而不是指针(我理解为首选)传入输入.你能告诉我我做错了什么吗?谢谢!

ThreeDoorCar myThreeDoorCar;
std::cout << myThreeDoorCar.getNumberOfDoors() << std::endl;

CarPrinter carPrinter(myThreeDoorCar);
carPrinter.printNumberOfDoors();
Run Code Online (Sandbox Code Playgroud)

c++

7
推荐指数
1
解决办法
2041
查看次数

C++重写的虚函数未被调用

我有以下设置:

main.cpp中:

int main()
{
    vector <Tour> tourList;
    Tour* tour_ptr;

    for (unsigned int i = 0; i < tourList.size(); i++)
    {
        tour_ptr = &tourList[i];
        tour_ptr->display();
    }
}
Run Code Online (Sandbox Code Playgroud)

Tour.h:

class Tour
{
   public:
    virtual void display();
};
Run Code Online (Sandbox Code Playgroud)

Tour.cpp:

void Tour::display()
{
    cout << "Tour ID: " << getID() << "\n";
    cout << "Description: " << getdescription() << "\n";
    cout << "Tour Fee: $" << getfee() << "\n";
    cout << "Total Bookings: " << getbookings() << "\n\n";
}
Run Code Online (Sandbox Code Playgroud)

GuidedTour.h:

class GuidedTour : …
Run Code Online (Sandbox Code Playgroud)

c++

7
推荐指数
2
解决办法
6209
查看次数

使用纯虚拟类复制和交换习语

我正在尝试使用纯虚方法和'复制和交换'惯用法实现虚拟类,但我遇到了一些问题.代码将无法编译,因为我在包含纯虚方法的类A的assign运算符中创建实例.

有没有办法如何使用纯虚方法和复制和交换成语?

class A
{
public:
    A( string name) :
            m_name(name) { m_type = ""; }
    A( const A & rec) :
            m_name(rec.m_name), m_type(rec.m_type) {}
    friend void swap(A & lhs, A & rhs)
    {
        std::swap(lhs.m_name, rhs.m_name);
        std::swap(lhs.m_type, rhs.m_type);
    }

    A & operator=( const A & rhs)
    {
        A tmp(rhs); 
        swap(*this, tmp);
        return *this;
    }

    friend ostream & operator<<( ostream & os,A & x)
    {
         x.print(os);
         return os;
    }

protected:
    virtual void print(ostream & os) =0;    

    string m_type;
    string m_name;
}; …
Run Code Online (Sandbox Code Playgroud)

c++ virtual class copy-and-swap

7
推荐指数
1
解决办法
882
查看次数

当auto遇到多态和虚函数时,什么是正确的行为?

class B {
    public:
    virtual void f(){
        printf("B\n");
    }
};
class D : public B { 
    public:
    void f() {
        printf("D\n");
    }
};

int main(void)  
{  
    B* d = new D();
    d->f();
    auto b = *d; 
    b.f();
}
Run Code Online (Sandbox Code Playgroud)

因为d->f();,输出是D.这是正确的.但是b.f();,输出是B.这是正确的吗?

c++ auto c++11

7
推荐指数
1
解决办法
441
查看次数

为什么我的C++对象丢失了它的VPTr

在调试程序的核心转储之一时,我遇到了多态的包含对象失去其VPTr的情况,我可以看到它指向NULL.

当一个对象丢失其VPTr时可能出现的情况.

先谢谢,Brijesh

c++ linux vptr

6
推荐指数
2
解决办法
2730
查看次数

多重继承类的复制构造函数

考虑一下这段代码:

#include <vector>
#include <iostream>
using namespace std;

class Base
{
    char _type;
public:
    Base(char type):
        _type(type)
    {}

    ~Base() {
        cout << "Base destructor: " << _type << endl;
    }
};

class uncopyable
{
    protected:
        uncopyable() {}
        ~uncopyable() {}
    private:
        uncopyable( const uncopyable& );
        const uncopyable& operator=( const uncopyable& );
};

class Child : public Base, private uncopyable
{
    int j;
public:
    Child():
        Base('c')
    {}
    ~Child() {
        cout << "Child destructor" << endl;
    }
};


int main()
{
    vector<Base> v; …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance stl multiple-inheritance copy-constructor

6
推荐指数
1
解决办法
1635
查看次数