如果我理解切片正确,我不认为这可能发生指针或智能指针.例如,如果你有:
class A
{
int something;
};
class B : public A
{
int stuff;
int morestuff;
};
int main()
{
    std::shared_ptr<B> b(new B());
    std::shared_ptr<A> a;
    a = b;
}
我的理解是,分配给"b"所指向的对象的内存块仍然相同,并且在分配给智能指针"a"时不会改变.
请确认或拒绝我的理解,或让我知道与此相关的任何陷阱.
我正在学习关于对象切片的艰难方法,我想知道指针是否有可能被对象切片.换一种说法:
让我们看下一个代码(例如):
class A {
    int n;
public:
    int f() const { return n; }
    void set(int i) { n = i; }
};
class B : public A {
public:
    int g() const { return f()+1; }
};
void h(const A& a) {
    a.f();
}
int main() {
    B b;
    A& a = b;
    A* ptrA = new B; 
    h(b);
  delete ptrA;
    return 0;
}
现在,让我们来看看这些代码:
A&a = b; //有"切片"吗?(为什么?)
A*ptrA =新B; //有"切片"吗?(为什么?)
A a = b; //有"切片"吗?(为什么?)
当我想要使用其中任何一个时,我真的不明白,或者当你不允许使用其中一个时.这些线之间的真正区别是什么..
考虑以下类:
class Coord
{
public:
    double _x, _y;
    Coord(double x, double y)
    {
        _x = x;
        _y = y;
    }
};
class NamedPoint : public Coord
{
public:
    int _id;
    NamedPoint(int id, double x, double y) :
        Coord(x,y),
        _id(id)
    {
    }
};
我想创建NamedPoint的成员函数--coord() - 返回与NamedPoint对应的Coord类型的引用.
例如,我想要像:
const Coord& NamedPoint::coord()
{
    return ((Coord)*this);
}
但我收到关于临时变量的警告,我并不为此感到高兴.
当然,以下工作:
Coord coord()
{
    Coord c = *this;
    return c;
}
但我宁愿返回一个参考.
有没有人知道使用继承类是否可行?
很抱歉没有解释该功能的要点.我为Coord和NamedPoint以不同的方式重载了==运算符.Coord只需检查{x,y},NamedPoint将检查{id,x,y}.如果我忘记在此==测试之前将一个NamedPoint强制转换为Coord,我将使用错误的版本.
所以,虽然我意识到这一点
(Coord)np1 == (Coord)np2 
会给我我想要的东西,我宁愿用类似的东西
np1.coord() == np2.coord()
我认为更清楚的是发生了什么.
为什么私有继承中不会发生对象切片?Static_cast 在这种情况下会出错? \n我知道私有继承不保留 \xe2\x80\x9cis - 其继承类之间的 \xe2\x80\x9d 关系。这是否与切片有关,因为派生不是基类型,因此编译器强制不进行隐式转换?
\n我有一个Mammal父类.
Dog,Cat,Lion是子类.
我使用向量来保存所有子类作为Mammal对象
vector<Mammal> v;
并使用此行将新对象附加到矢量.
v.push_back(new Dog(Name, Blue, Owner));
显然它不起作用.Error no instance of overload function在编译期间向我抛出.我是C++的新手所以我不确定动态创建父类数组以保存所有子对象的正确方法是什么
我有一个看起来像这样的课程:
class MyClass {
    public:
    void doSomething() { // nothing here };
}
它还有一个看起来像这样的子类
class MyChildClass : MyClass {
    public:
    void doSomething() { // actual code here };
}
正如您所看到的,该doSomething()函数在父类中不执行任何操作,但子类会覆盖它并添加实际代码.我的问题是我试图做这样的事情:
MyClass foo = MyChildClass();
foo.doSomething();
我很震惊地发现,在这种情况下,MyClass而不是MyChildClass的版本doSomething()被称为,即使FOO实际类型为MyChildClass.我在Objective-C方面的经验远远超过C++,所以这对我来说非常奇怪.似乎C++正在确定doSomething()在编译时需要调用哪个版本的版本,而不是在运行时检查对象的类型并调用正确的版本.
这对我来说是有问题的,因为在我的实际代码中,我拥有的是一个父类和从中继承的多个不同的子类.这些子类中的每一个都doSomething()使用它们自己的唯一实现来覆盖该函数.我最终拥有一个std::vector完整的MyClass对象(它们实际上充满了各自继承的许多不同类型的对象MyClass),我想循环遍历这些对象并调用它们的版本,doSomething()而不是在编译时实际知道它们的类型.在Objective-C中这很容易,但是我有什么方法可以在C++中实现这一点吗?
在对象切片中,当派生类对象复制到基类对象时,派生类的_vptr是否也会像基类的其他成员一样复制到基类的_vptr?如果没有,为什么?
class Base {
public :
    virtual void Display() { cout << "In Base" << endl; }
};
class Derived:public Base {
public:
    void Display() { cout << "In Derived" << endl; }
};
int main()
{
    Derived objD;
    Base objB;
    objB = objD;
    objB.Display();
}
我观察到上述代码片段的以下结果。
Output
In Base
如果我这样做:
//... in .h header:
class MyCustomException : public std::exception{
  private:
    string message;
  public:
    MyCustomException (string msg); //in .cpp implements "message = msg"
    ~MyCustomException() throw(){ }
    string what(); //implemented to return message.
}
//... other code ... in cpp:
if (<blah blah blah>){
  throw MyCustomException("I want to see THIS message when I fail ... but I don't...");
}
//... more code, in cpp:
//try{<blah...>}
catch(const:: std::exception& e){
   cout << e.what() << endl << flush; //just prints "std::exception" ... same if …我现在很迷茫.我做了一个矢量课.一切都按照我希望的方式运作,直到最后.当调用析构函数时,我收到一条错误消息:Debug assertion failed BLOCK_TYPE_IS_VALID(pHead-> nblockuse).我在SO上看过很多类似这样的问题,但我所尝试的却没有用.
.h的一部分.
private:
    int* _myArray;
    int _size;
    int _capacity;
#include "File.h"
 const string RETURN_CARRIAGE_STR = "\n";
 const string SIZE_STR = "Size ";
 const string CAPACITY_STR = "Capacity ";
 const int INITIAL_CAPACITY = 2;
 int main(void)
{
cout << "\nCreating a vector Sam of size 4.";
MyVector sam( 4 );
cout << "\nPush 12 values into the vector.";
for (int i = 0; i < 12; i++)
    sam.push_back(i);
cout << "\nHere is sam: ";
cout << sam; …我有以下代码:
#include <iostream>
class AnimalRepository {
public:
    virtual ~AnimalRepository() = default;
    virtual auto makeSound() -> void {
        std::cout << "Null" << std::endl;
    }
};
class CatRepository: public AnimalRepository {
public:
   
    auto makeSound() -> void override {
       std::cout << "Meow" << std::endl;
    }
};
class DogRepository: public AnimalRepository {
public:
    auto makeSound() -> void override {
        std::cout << "Woof" << std::endl;
    }
};
class Animal {
public:
    explicit Animal(const AnimalRepository& repository)
            : repository(repository) {
    }
    auto makeSound() -> void {
        return …c++ overriding virtual-functions repository-pattern object-slicing
c++ ×11
object-slicing ×11
inheritance ×3
class ×2
pointers ×2
c++98 ×1
destructor ×1
exception ×1
memory-leaks ×1
overriding ×1
reference ×1
vptr ×1
vtable ×1