小编Per*_*kie的帖子

静态和动态演员之间的区别

这个类是多态的.为什么两者都打印相同的输出?

class A
{
public:
    virtual void P(){ cout << "A" << endl; }

};
class B : public A
{
public:
    void P()override{
        cout << "B" << endl;
    }
    B(){ cout << "Created B" << endl; s = "Created by B"; }
    string s;
};
Run Code Online (Sandbox Code Playgroud)

主要:变体1:

A* a = new B();    // Created B
B* b = static_cast<B*>(a);
b->P();    B
cout<<b->s<<endl;  // Created by B
Run Code Online (Sandbox Code Playgroud)

变种2:

A* a = new B();
    B* b = dynamic_cast<B*>(a);
    if (b){
        b->P();
        cout …
Run Code Online (Sandbox Code Playgroud)

c++ dynamic-cast static-cast

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

将'std :: initializer_list <int>'转换为'int'

为什么我不能使用"initializer_list"作为"普通"变量分配变量?

码:

void stovr(int a){}
int main() {

   auto v {5}; // v is std::initializer_list<int>
   stovr(v);  // cannot convert 'std::initializer_list<int>' to 'int'

}
Run Code Online (Sandbox Code Playgroud)
  • 为什么没有隐含的对话?(编辑:因为v是列表)))
  • 为什么vlist和不是int

c++ initializer-list c++11

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

C++删除指针两次

我知道当两个指针寻址同一个动态分配的对象时,会发生"删除相同的内存两次"错误.如果delete将其应用于其中一个指针,则将对象的内存返回到免费存储区.如果我们随后删除第二个指针,则免费存储可能已损坏.

但为什么这段代码不会导致运行时错误?

 string *str_1 = new string;
  auto str_2 = str_1;
  *str_1 = "AAA";
  cout<<*str_2<<endl;
  delete str_1;
  delete str_2;  // No Error

    // Prints AAA
Run Code Online (Sandbox Code Playgroud)

c++ pointers

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

类本身内部的实例

此代码有效:

class Person{
    public Person p;
    public string name;
    private int age;

}
class Solution {


    static void Main(String[] args) {

        Person z = new Person () { name = "Stacl"};
        Console.WriteLine (z.name);
        Person a = new Person ();
        Console.WriteLine (a.name);

    }
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用:

class Person{
    public Person p = new Person (){name = "Inside",age = 45}; // add 'new'
    public string name;
    private int age;

}
class Solution {


    static void Main(String[] args) {

        Person z = new Person …
Run Code Online (Sandbox Code Playgroud)

c# oop instance

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

C++ vector.begin()和vector [0]

为什么这两行打印不同的地址?

vector<int> v1{ 12,2,34 };
printf_s("%d -  0x%p\n", v1[0], &v1[0]);
printf_s("%d -  0x%p\n",*v1.begin(), v1.begin());
Run Code Online (Sandbox Code Playgroud)

此地址中的值相同,但地址本身不同.这是否意味着同一个数组有两个副本?

编辑:在调试模式下它打印不同的地址,在发布模式下有相同的地址:)

c++ vector

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

C#语法错误

是语法错误还是编译错误?

using System;

namespace AAA
{
    class MyException : Exception{

    }
    class My2Exception : MyException{

    }
    class MainClass
    {
        public static void Main (string[] args)
        {
            try{
                throw new MyException();
            }
            catch(Exception e){  // compiler says that this catch all exception occur          error? Is it syntax error?
            }
            catch(MyException m){  // Syntax error
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是语法错误吗?这个错误语法错误了吗?谢谢

c# exception

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

为什么我无法访问列表实例?

为什么我不能使用这个实例?

    class MainClass
    {

        List<int> d = new List<int> (5);
        // d[0] error
        public static void Main (string[] args)
        {
            // d[0] error 
        }
    }
Run Code Online (Sandbox Code Playgroud)

为什么我不能使用这个对象?

c# list

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