C++不允许基于返回类型的方法的多态性.但是,当重载隐式转换成员函数时,这似乎是可能的.
有谁知道为什么?我认为运算符在内部处理方式.
编辑:这是一个例子:
struct func {
operator string() { return "1";}
operator int() { return 2; }
};
int main( ) {
int x = func(); // calls int version
string y = func(); // calls string version
double d = func(); // calls int version
cout << func() << endl; // calls int version
}
Run Code Online (Sandbox Code Playgroud) 对于其clas(子)具有单继承的对象,通常需要多少个vptrs,其基类为多个继承base1和base2.确定对象提供了多少个vpt的策略是什么,它提供了几个单继承和多继承.虽然标准没有指定vptrs,但我只是想知道一个实现如何实现虚函数.
我有一个函数,它接受一个unsigned long*并且需要将它传递给一个带有unsigned int*的外部库,并且在这个平台上unsigned int/long的大小相同.
void UpdateVar(unsigned long* var) {
// this function will change the value at the address of var
ExternalLibAtomicUpdateVar((unsigned int*)var); // lib atomically updates variable
}
Run Code Online (Sandbox Code Playgroud)
这会产生一个警告,说它违反了严格别名规则.有没有工作?
谢谢
编辑:我为不清楚而道歉.代码是一个原子更新,因此绕过库来存储它不是一个选项.我可以下载到汇编但我想在C++中这样做.
我惊讶地发现"const"中的这个"漏洞":
#include <stdio.h>
class A
{
int r ;
public:
A():r(0){}
void nonconst()
{
puts( "I am in ur nonconst method" ) ;
r++;
}
} ;
class B
{
A a ;
A* aPtr ;
public:
B(){ aPtr = new A() ; }
void go() const
{
//a.nonconst() ; // illegal
aPtr->nonconst() ; //legal
}
} ;
int main()
{
B b ;
b.go() ;
}
Run Code Online (Sandbox Code Playgroud)
因此,基本上从const方法B::go(),如果指针引用了类型的对象,则可以调用非const成员函数(恰当地命名nonconst())A.
这是为什么?看起来像一个问题(它在我的代码中,我找到了它.)
假设我需要执行以下操作(这只是用于讨论C++标准的一些富有想象力的代码,因此我不会讨论为什么我这样设计它,所以不要打扰我:你的设计是错误的.)
T* ptr = new T;
shared_ptr<T> p(ptr);
shared_ptr<T> q(ptr, SomeDeleterThatDoesnotDeleteButDoSomeOtherStuff());
Run Code Online (Sandbox Code Playgroud)
假设逻辑保证p其副本或其某些副本比所有副本的寿命更长q,因此实际上不会有任何问题.我的问题是,C++标准是禁止的,例如C++标准明确表示为UB,不同的shared_ptr计数器共享相同的地址?
谢谢.
是否可以在联合中共享两个数组,如下所示:
struct
{
union
{
float m_V[Height * Length];
float m_M[Height] [Length];
} m_U;
};
Run Code Online (Sandbox Code Playgroud)
这两个阵列共享相同的内存大小还是其中一个更长?
我正在尝试在声明时初始化以下union数组:
typedef union { __m128d m; float f[4]; } mat;
mat m[2] = { {{30467.14153,5910.1427,15846.23837,7271.22705},
{30467.14153,5910.1427,15846.23837,7271.22705}}};
Run Code Online (Sandbox Code Playgroud)
但是我发现了以下错误:
matrix.c: In function ‘main’:
matrix.c:21: error: incompatible types in initialization
matrix.c:21: warning: excess elements in union initializer
matrix.c:21: warning: (near initialization for ‘m[0]’)
matrix.c:21: warning: excess elements in union initializer
matrix.c:21: warning: (near initialization for ‘m[0]’)
matrix.c:21: warning: excess elements in union initializer
matrix.c:21: warning: (near initialization for ‘m[0]’)
matrix.c:21: error: incompatible types in initialization
matrix.c:21: warning: excess elements in union initializer
matrix.c:21: …Run Code Online (Sandbox Code Playgroud) 当我尝试编译以下代码时:
class A {
public:
A(int v) : virt(v) { }
int virt;
int getVirt(void) const { return virt; }
};
class B : private virtual A {
protected:
B(int v) : A(v) { }
using A::getVirt;
};
class C : public B, private virtual A {
protected:
C(int v) : A(v), B(v) { }
using A::getVirt;
};
class D : public C {
public:
D(void) : C(3) { }
using C::getVirt;
};
#include <iostream>
int main(int argc, char *argv[]) …Run Code Online (Sandbox Code Playgroud) 在这段代码中:
class Base {
public:
virtual void method() = 0;
};
class Derived1 : public Base{
public:
virtual void method() override { }
};
class Derived2 : public Base{
public:
void method() override { }
};
Run Code Online (Sandbox Code Playgroud)
有什么区别Derived1和Derived2?
更模糊的困境......我喜欢C++,但有时我讨厌它.
我无法弄清楚为什么编译器在这里抱怨,以及我能做些什么呢.
struct blah
{
template<class t>
blah(void(*)(t), t){}
};
void Func(int i) {}
void Func2(int& i) {}
void test()
{
int i = 3;
blah b(Func, i);
blah b2(Func2, i); //error C2660: 'blah::blah' : function does not take 2 arguments
blah b3(Func2, (int&)i); //error C2660: 'blah::blah' : function does not take 2 arguments
}
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
我正在使用MSVC2008.
c++ ×9
c++11 ×2
unions ×2
arrays ×1
c ×1
const ×1
constructor ×1
initializer ×1
overriding ×1
pointers ×1
polymorphism ×1
reference ×1
shared-ptr ×1
sse ×1
template-argument-deduction ×1
templates ×1
type-punning ×1
vptr ×1
vtable ×1