小编cur*_*guy的帖子

具有私有基础的函数成员指针

以下代码产生编译时错误:

' base::print':无法访问类中声明的私有成员' base_der'

但是,我已public在派生类中创建了该成员.为什么这不起作用?

#include <iostream>

using namespace std;

class base
{
public:
    int i;
    void print(int i)
    {
        printf("base i\n");
    }
};

class base_der : private base
{
public:
    using base::print;
};

int main()
{
    // This works:
    base_der cls;
    cls.print(10);

    // This doesn't:    
    void (base_der::* print)(int);
    print = &base_der::print; // Compile error here
}
Run Code Online (Sandbox Code Playgroud)

c++ member-function-pointers member-pointers implicit-conversion private-inheritance

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

虚拟继承混淆

我正在阅读关于继承的内容,我有一个我几个小时都无法解决的重大问题:

鉴于一个类Bar是一个具有virtual函数的类,

class Bar
{
    virtual void Cook();
};
Run Code Online (Sandbox Code Playgroud)

有什么不同:

class Foo : public Bar
{
    virtual void Cook();
};
Run Code Online (Sandbox Code Playgroud)

和

class Foo : public virtual Bar
{
    virtual void Cook();
};
Run Code Online (Sandbox Code Playgroud)

?谷歌搜索和阅读的时间提供了大量有关其用途的信息,但实际上没有人告诉我两者之间有什么区别,只是让我更加困惑.

c++ inheritance virtual-inheritance c++11

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

从构造函数调用虚函数和纯虚函数

当我从基础构造函数调用虚函数时,编译器不会给出任何错误.但是当我从基类构造函数调用纯虚函数时,它会产生编译错误.

考虑下面的示例程序:

#include <iostream>

using namespace std;
class base
{
   public:
      void virtual virtualfunc() = 0;
      //void virtual virtualfunc();
      base()
      {
         virtualfunc();
      }
};

void base::virtualfunc()
{
   cout << " pvf in base class\n";
}

class derived : public base
{
   public:
   void virtualfunc()
   {
      cout << "vf in derived class\n";
   }
};

int main()
{
   derived d;
   base *bptr = &d;
   bptr->virtualfunc();

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

这里可以看出纯虚函数有一个定义.我期望在bptr->virtualfunc()执行时调用基类中定义的纯虚函数.相反,它给出了编译错误:

错误:从构造函数调用的抽象虚拟`virtual void base :: virtualfunc()'

这是什么原因?

c++ constructor virtual-functions compiler-errors dynamic-binding

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

在C++中构造对象时有两个隐式转换是否有效?

鉴于以下两个构造函数签名,是否可以构造一个Couplewith Couple("George", "Nora")?我的编译器抱怨下面显示的错误.如果我用Couple(std::string("George"), std::string("Nora"))它调用它编译好.我猜这是一个隐式转换的问题让我感到惊讶,因为我虽然char*to string也没关系.

class Person
{
    public:
        Person(const std::string& name);
};

class Couple
{
    public:
        Coordinate(const Person& p1, const Person& p2, const Optional<Person>& = Optional<Person>());
};

TestCouple.cpp:69: error: no matching function for call to `Couple::Couple(const char[7], const char[5])'
TestCouple.h:24: note: candidates are: Couple::Couple(const Person&, const Person&, const Optional<fox::Person>&)
Run Code Online (Sandbox Code Playgroud)

c++ constructor implicit-conversion

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

istream会发生什么隐式转换?

我发现了一张C-ISH C++代码,并问自己(略学术)的问题,有什么隐式类型转换发生在这里,以在到达bool该if要求?

int val;
if( (std::cin >> val) == 0 )
    ...
Run Code Online (Sandbox Code Playgroud)

我到目前为止:

  • std::cin >> val返回(参考)cin,因此istream&
  • 因此==接收cin和0作为操作数,即istream和int

我认为没有bool operator==(istream&, int)可用的(也没有相应的成员函数istream),所以是否涉及转换?


为了清楚起见,程序员的意图是检查输入是否成功,即应该写入if(!(std::cin >> val)).

c++ boolean istream implicit-conversion

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

模板函数专业化与重载

从一些关于模板专业化的幻灯片:

#include <iostream>

using namespace std;

template<class X> 
X& min(X& a, X& b)
{
    return a > b ? b : a;
}

int& min(int& a, int & b)
{
    // rewrite of the function in the case of int:
    cout << "int explicit function\n";
    return a > b ? b : a;
}

/* 
new syntax – the more appropriate way:
template<>
int& min<int>(int& a, int& b)
{
    cout << "int explicit function\n";
    return a > b ? b …
Run Code Online (Sandbox Code Playgroud)

c++ templates overloading specialization template-function

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

什么时候不应该使用 [[carries_dependency]]?

我发现了一些问题(比如这个)问什么[[carries_dependency]]是,这不是我在这里问的。

我想知道你什么时候不应该使用它,因为我读过的所有答案都让人觉得你可以把这段代码贴在任何地方,而且你会神奇地得到相等或更快的代码。一个评论说代码可以相等或更慢,但海报没有详细说明。

我想在任何函数返回或参数上使用 this 的合适位置是指针或引用,并且将在调用线程内传递或返回,并且不应在回调或线程入口点上使用它。

有人可以评论我的理解并详细说明一般的主题,何时以及何时不使用它?

编辑:我知道这个主题有这本书,如果其他读者感兴趣的话;它可能包含我的答案,但我还没有机会通读它。

c++ multithreading memory-model carries-dependency stdatomic

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

C++ weak_ptr创建性能

我已经读过创建或复制std :: shared_ptr涉及一些开销(参考计数器的原子增量等).

但是如何从它创建一个std :: weak_ptr:

Obj * obj = new Obj();
// fast
Obj * o = obj;
// slow
std::shared_ptr<Obj> a(o);
// slow
std::shared_ptr<Obj> b(a);
// slow ?
std::weak_ptr<Obj> c(b);
Run Code Online (Sandbox Code Playgroud)

我希望在一些更快的性能,但我知道共享指针仍然必须递增弱引用计数器..所以这仍然像将shared_ptr复制到另一个慢?

c++ performance reference-counting shared-ptr weak-ptr

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

什么时候应该使用静态数据成员与const全局变量?

声明const全局变量已证明对确定API的某些功能参数很有用.例如,在我的API上,运算符的最小数值精度顺序为2; 因此,我宣布:

const int kDefaultOrderAccuracy{2};
Run Code Online (Sandbox Code Playgroud)

作为全球变量.将它作为static const描述这些运算符的类的公共数据成员会更好吗?一般来说,选择一个比另一个更好?

declaration global-variables static-members linkage c++11

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

变量名称与函数名称相同,给出编译器错误...为什么?

今天进入一个有趣的问题,我试图理解为什么.

考虑以下:

class Base
{
public:
    Base(){}
    ~Base(){}
    static void function1(){}
        void function2()
        {
           int function1;
           function1 = 0;
           function1();   //<-compiler error
           function1 = 1;
        }
};
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

表观调用括号前的表达式必须具有(指向 - )函数类型

我想我明白为什么我收到这个错误:

  1. 当function1它本身被调用时function2(),它实际上是一个函数指针function1().

  2. 里面的范围function2,当int function1被宣布," function1变"阴影" function1函数指针".

  3. 当function1()在内部调用时function2(),它假定function1是变量并且给出错误.

  4. 通过调用Base::function1();内部来解决这个问题function2().

我的问题是:为什么编译器在声明时没有出错int function1;?这不应该被允许吗?

c++ scope declaration name-lookup name-hiding

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