小编EOG*_*EOG的帖子

c ++ - 从抽象类继承的declering继承的抽象方法是否有任何性能问题?

我有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)

我的问题是:GrandParentMethodParent类中进行Declering 使得Child在一段时间后实现类更容易(只需要为抽象方法检查一个标头),但它是否有任何性能(内存?cpu?)问题?

c++ performance abstract-class

3
推荐指数
1
解决办法
87
查看次数

通过迭代器进行basic_string转换是否合法?

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++ string string-conversion c++11

3
推荐指数
1
解决办法
144
查看次数

Java - Java有类似C#的struct自动构造函数

我已经使用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在声明时使用默认构造函数?

c# java constructor

2
推荐指数
1
解决办法
1519
查看次数

Typescript - 静态属性创建优化

我是JavaScript的新手,因为我来自强类型语言,所以我选择了TypeScript.

我想知道发生了什么变量redRed 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)

javascript typescript

2
推荐指数
1
解决办法
4418
查看次数

删除void指针时不调用析构函数

我有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++ containers pointers destruction

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

C++ - 模板函数调用运算符重载 - 错误C2064

我试图在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)

c++ templates operator-overloading function-call-operator

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

找到三角形2D中任何点的最快方法

我需要一种最快的方法来在2D中找到三角形(而不是边缘)内的任何点.有帮助吗?

algorithm performance

0
推荐指数
1
解决办法
2365
查看次数

如何在运行时检查void*是否为IUnknown*?

如何在运行时检查void*是否为IUnknown*?

IUnknown *unk = dynamic_cast<IUnknown*>(item);
Run Code Online (Sandbox Code Playgroud)

不起作用(编译错误).

c++ windows

0
推荐指数
1
解决办法
228
查看次数

c ++ final非虚拟类是否被重新解释为数组安全使用?

我有一个非虚拟的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++ arrays pointers reinterpret-cast c++11

0
推荐指数
1
解决办法
150
查看次数

c ++ - 抛出自定义空指针异常

有一种简单的方法可以在c ++中抛出自定义空指针异常吗?我的想法是重新定义this指针,但它有3个问题:

  1. 不使用thisthrows标准Acces Violation Exception
  2. 每次使用时都会检查指针
  3. Visual 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++ exception-handling exception this

-1
推荐指数
1
解决办法
1125
查看次数