假设我们有一个base
班级和一个班级derived
.所以:
class base {
protected:
~base(){
//...
}
// ...
};
class derived : public base {
// ...
};
Run Code Online (Sandbox Code Playgroud)
现在说我们使用上面的类和智能指针类有这个代码:
SmartPointer<base> bptr(new derived());
delete bptr;
Run Code Online (Sandbox Code Playgroud)
我知道它会derived
通过调用析构函数来阻止对象的切片derived
,但它如何知道呢?存储在智能指针中的引用不会是类型的引用base*
吗?它是否遍历某种层次结构树,将该指针强制转换为derived*
然后调用delete?还是有其他一些我不知道的事情?
该实现被认为是线程安全的,非侵入式的和引用计数.
是的,你看到的类类似于我正在测试的类.显然有一种方法可以使用THESE GIVEN课程.关于如何在上面的问题中提到的主要想法,但我不确定这样的实现如何工作.
会有切片的危险吗?
result Compare(const Osp::Base::Object &obj1, const Osp::Base::Object &obj2, int &cmp) const {
cmp = ((const Block)obj1).NumSuperBlocks() - ((const Block)obj2).NumSuperBlocks();
}
Run Code Online (Sandbox Code Playgroud)
哪里
class Block : Object {/*Filler*/}
Run Code Online (Sandbox Code Playgroud)
并obj1
和obj2
放心是Block
对象?
我很想用:
cmp = ((const Block*)&obj1)->NumSuperBlocks() - ((const Block*)&obj2)->NumSuperBlocks();
Run Code Online (Sandbox Code Playgroud)
但在阅读SO的对象切片标签的简要描述时,我很想使用前者.但我真的不想要任何讨厌的无声切片.
请参见以下代码:
#include <iostream>
#include <chrono>
class Parent
{
public:
Parent() = default;
virtual ~Parent() = default;
Parent(const Parent& pr) : i{pr.i} {std::cout << "Parent copy constructor\n";}
Parent& operator=(const Parent& pr) {std::cout << "Parent copy assignment\n"; this->i = pr.i; return *this;}
Parent(Parent&& pr) : i{std::move(pr.i)} {std::cout << "Parent move constructor\n";}
Parent& operator=(Parent&& pr) {std::cout << "Parent move assignment\n"; this->i = std::move(pr.i); return *this;}
virtual void print_i_j() = 0;
int i = 10;
};
class Child : public Parent
{
public:
Child() …
Run Code Online (Sandbox Code Playgroud) 我想问一下,当我使用没有指针的虚函数时会发生什么?例如:
#include <iostream>
using namespace std;
class Parent
{
public:
Parent(int i) { }
virtual void f() { cout<<"Parent"<<endl; }
};
class Child : public Parent
{
public:
Child(int i) : Parent(i) { }
virtual void f() { Parent::f(); cout<<" Child"<<endl; }
};
int main()
{
Parent a(2);
Parent b = Child(2);
a.f();
b.f();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
^^为什么不起作用?我在哪里可以找到有关虚拟方法如何工作的内容?
我主要有以下内容:
Sum *sum = new Sum(Identifier("aNum1"), Identifier("aNum2"));
Run Code Online (Sandbox Code Playgroud)
我的课程是:
class Table {
private:
static map<string, int> m;
public:
static int lookup(string ident)
{
return m.find(ident)->second;
}
static void insert(string ident, int aValue)
{
m.insert(pair<string, int>(ident, aValue));
}
};
class Expression {
public:
virtual int const getValue() = 0;
};
class Identifier : Expression {
private:
string ident;
public:
Identifier(string _ident) { ident = _ident; }
int const getValue() { return Table::lookup(ident); }
};
class BinaryExpression : public Expression {
protected:
Expression …
Run Code Online (Sandbox Code Playgroud) 在将源代码编译为.o文件并使用"ar rcs libMyLibrarylib.a*.o"来创建库之后我得到了段错误,因为我使用的是带有成员变量和私有函数的头文件.当我使用完全相同的标题时,我没有得到错误.删除地图中的指针时会发生段错误.
#include <**Type**>
class A
{
public:
A();
~A(); //In the destructor I iterate through the map to free everything before
void function();
private:
void privateFunction();
std::map<**Type**, int*> myMap;
}
Run Code Online (Sandbox Code Playgroud)
class A
{
public:
A();
~A();
void function();
}
Run Code Online (Sandbox Code Playgroud)
没有使用确切的头文件时是否有切片或其他东西?我想隐藏任何使用lib 的#include of Type.
我有库的单元测试,它不是段错误但它使用与编译它相同的头文件.
我从以下代码中获得了意外行为:
struct Base
{
Base() {}
virtual ~Base() {}
virtual void foo() const = 0;
protected:
Base(const Base &) {}
};
struct Derived : public Base
{
Derived() {}
Derived(const Derived &other) : Base(other) {}
virtual void foo() const {}
};
struct NewDerived
{
operator const Derived() { return Derived(); }
};
void func(const Base &b)
{
b.foo();
}
int main()
{
func(NewDerived());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用MSVC2008,我在main()中得到此编译错误:
error C2248: 'Base::Base' : cannot access protected member declared in class 'Base'
Run Code Online (Sandbox Code Playgroud)
为什么要尝试访问Base的复制构造函数? …
我有这样的事情:
#include <iostream>
class X;
class A {
public:
virtual void bar(X &x);
};
class B : public A {
public:
};
class X {
public:
void foo(A &a) { std::cout << "foo A" << std::endl; }
void foo(B &b) { std::cout << "foo B" << std::endl; }
};
void A::bar(X &x) { x.foo(*this); }
int main(int argc, char **argv) {
X x;
B b;
b.bar(x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译并执行它,你将拥有:
# ./a.out
foo A
#
Run Code Online (Sandbox Code Playgroud)
我相信这是因为对象在被投射到A时会被切掉.我怎么能避免这样做,所以我得到了
foo B
Run Code Online (Sandbox Code Playgroud)
没有在B中实现方法或使用奇怪的重复模板模式 …
我想在a中存储具有相同基类的多个类std::vector
.经过一些研究,我很明显我必须使用指针来防止对象切片.但是,当我创建向量时,向其添加元素并返回它,结果向量没有正确的值.
举个例子,这是我的两个类:
class Base {
public:
int var0;
}
class Derived : public Base {
public:
int var1;
}
Run Code Online (Sandbox Code Playgroud)
这是一个简单的print
功能.作为一项规则,所有实例Base
应该有var0 == 23
,和所有实例Derived
应该有var0 != 23
.
void print(Base& value) {
if (value.var0 == 23) {
std::cout << "Base: " << value.var0 << std::endl;
} else {
Derived d = (Derived&) value;
std::cout << "Derived: " << d.var0 << ", " d.var1 << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
首先,这确实像我想要的那样:
int main() …
Run Code Online (Sandbox Code Playgroud) 这是一个示例C++问题,以找出结果.
#include <iostream>
#include <vector>
class A
{
public:
A(int n = 0) : m_n(n) { }
public:
virtual int f() const { return m_n; }
virtual ~A() { }
protected:
int m_n;
};
class B
: public A
{
public:
B(int n = 0) : A(n) { }
public:
virtual int f() const { return m_n + 1; }
};
int main()
{
const A a(1);
const B b(3);
const A *x[2] = { &a, &b };
typedef std::vector<A> …
Run Code Online (Sandbox Code Playgroud) c++ ×10
object-slicing ×10
c++11 ×2
constructor ×1
downcast ×1
inheritance ×1
iterator ×1
overloading ×1
polymorphism ×1
stl ×1
vector ×1