我有3节课
class GrandParent
{
virtual int GrandParentMethod() = 0;
};
class Parent : public GrandParent
{
virtual int ParentMethod() = 0;
virtual int GrandParentMethod() = 0;
};
class Child : public Parent
{
int ParentMethod() { return 1; }
int GrandParentMethod() { return 0; }
};
Run Code Online (Sandbox Code Playgroud)
我的问题是:GrandParentMethod在Parent类中进行Declering 使得Child在一段时间后实现类更容易(只需要为抽象方法检查一个标头),但它是否有任何性能(内存?cpu?)问题?
iterator是basic_strings之间的propper转换吗?这样安全/合法吗?这会创建一个有效的字符串吗?
std::u16string str16;
//str16 is set here;
std::string cStr(str16.cbegin(), str16.cend());
Run Code Online (Sandbox Code Playgroud)
在VS 2013中它似乎工作正常.
我已经使用C#很长一段时间了,现在我需要用Java做一些事情.
在java中有类似C#的struct自动构造函数吗?
我的意思是在C#
struct MyStruct
{
public int i;
}
class Program
{
void SomeMethod()
{
MyStruct mStruct; // Automatic constructor was invoked; This line is same as MyStruct mStruct = new MyStruct();
mStruct.i = 5; // mStruct is not null and can i can be assigned
}
}
Run Code Online (Sandbox Code Playgroud)
是否可以强制java在声明时使用默认构造函数?
我是JavaScript的新手,因为我来自强类型语言,所以我选择了TypeScript.
我想知道发生了什么变量red在Red getter这个代码.它会在每次调用时重新创建(如in Green getter)还是每次创建一次并使用?哪个最好?
class Color {
public R: number;
public G: number;
public B: number;
public A: number;
static get Red(): Color {
var red = new Color(255, 0, 0);
Color.Red = function() { return red; }
return red;
}
static get Green(): Color {
return new Color(0, 255, 0);
}
constructor(red: number, green: number, blue: number, alpha: number = 255) {
this.R = red;
this.G = green;
this.B = blue;
this.A = …Run Code Online (Sandbox Code Playgroud) 我有3节课
class A
{
A();
virtual ~A();
}
class B : public A
{
B();
~B();
}
class C
{
void *obj;
C() : obj(nullptr) {}
~C() { if (obj) delete obj; }
}
Run Code Online (Sandbox Code Playgroud)
当我使用class C作为类的任何子类的容器A并尝试删除C实例时.A,B析构函数是不是很正常?什么是solutuon?
C* instance = new C();
instance.obj = new B();
//Magic
delete instance; // A and B destructor is not called
Run Code Online (Sandbox Code Playgroud) 我试图在c ++中重载函数调用操作符,我得到了这个无法解决的编译错误(Visual Studio 2010).
错误符合要求 act(4);
#include <stdio.h>
#include <iostream>
void Test(int i);
template <class T> class Action
{
private:
void (*action)(T);
public:
Action(void (*action)(T))
{
this->action = action;
}
void Invoke(T arg)
{
this->action(arg);
}
void operator()(T arg)
{
this->action(arg);
}
};
int main()
{
Action<int> *act = new Action<int>(Test);
act->Invoke(5);
act(4); //error C2064: term does not evaluate to a function taking 1 arguments overload
char c;
std::cin >> c;
return 0;
}
void Test(int i)
{
std::cout << …Run Code Online (Sandbox Code Playgroud) 如何在运行时检查void*是否为IUnknown*?
IUnknown *unk = dynamic_cast<IUnknown*>(item);
Run Code Online (Sandbox Code Playgroud)
不起作用(编译错误).
我有一个非虚拟的final类,它只声明了相同的类型字段.
struct Vector3 final
{
float X, Y, Z;
Vector3(float x, float y, float z) : X(x), Y(y), Z(z)
{
}
float Sum()
{
return X + Y + Z;
}
};
Run Code Online (Sandbox Code Playgroud)
将指向此类实例的指针重新解释为浮点数组是否安全?
int main(int argc, const char *argv[])
{
Vector3 v(10, 20, 30);
Vector3 *pV = &v;
float *ff = reinterpret_cast<float*>(pV);
std::cout << ff[0] << std::endl << ff[1] << std::endl << ff[2] << std::endl;
char c;
std::cin >> c;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 有一种简单的方法可以在c ++中抛出自定义空指针异常吗?我的想法是重新定义this指针,但它有3个问题:
thisthrows标准Acces Violation ExceptionVisual Studio将此显示为InteliSense错误(可编译)(不知道其他编译器做了什么)
#include <iostream>
#define this (this != nullptr ? (*this) : throw "NullPointerException")
class Obj
{
public:
int x;
void Add(const Obj& obj)
{
this.x += obj.x; // throws "NullPointerException"
//x = obj.x; // throws Access Violation Exception
}
};
void main()
{
Obj *o = new Obj();
Obj *o2 = nullptr;
try
{
(*o2).Add(*o);
}
catch (char *exception)
{
std::cout << exception;
}
getchar();
}
Run Code Online (Sandbox Code Playgroud)c++ ×7
c++11 ×2
performance ×2
pointers ×2
algorithm ×1
arrays ×1
c# ×1
constructor ×1
containers ×1
destruction ×1
exception ×1
java ×1
javascript ×1
string ×1
templates ×1
this ×1
typescript ×1
windows ×1