小编Vik*_*ngh的帖子

::范围解析运算符在c ++中的模板函数调用之前

我坚持使用模板和范围解析运算符.我在文件中找到了这些行,我无法弄清楚为什么我们在模板函数调用前使用::据我所知,我们只能在引用全局变量时使用::在变量前面.任何想法都会有所帮助

#define CREATE_AND_DECODE_TYPE(Type, buffer, pType) \
    ::CreateAndDecodeType<Type>(buffer, pType, throwVarBindExceptions, static_cast<Type *>(NULL))
Run Code Online (Sandbox Code Playgroud)

c++ templates scope-resolution

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

在成员函数中使用'delete this'后,我可以访问其他成员函数.为什么?

我刚刚编写了一个示例程序来查看删除它的行为

class A
 {
   ~A() {cout << "In destructor \n ";}
 public: 
    int a;
    A() {cout << "In constructor \n ";}

    void fun()
     {
       cout << "In fun \n";
       delete this;
       cout << this->a << "\n"; // output is 0
       this->fun_2(); // how m able to call fun_2, if delete this is called first ??
     }

   void fun_2()
    {
      cout << "In fun_2 \n";
    }

main()
{
 A *ptr = new A;
 ptr->a = 100;
 ptr->fun(); //delete this …
Run Code Online (Sandbox Code Playgroud)

c++ linux oop this-pointer

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

如何从基类和派生类调用函数?

我正在编写一段简单的代码。

class A
 {
   public:
   virtual func ()
    { // allocate memory 
    }
 };

class B : public A
 {
   public:
   func ()
    { // some piece of code
      // but call base class same function Ist 
    }
 }

 main()
    {
      A *ptr = new B;
      ptr->func () //here I want to call base class function first 
                   //and then derived class function
                   // How to implement ??
    }
Run Code Online (Sandbox Code Playgroud)
  1. 如何先调用基类函数,然后从派生类调用相同的函数??我不想显式调用每个函数,我只会调用派生类函数,并且应该自动调用基类函数。

  2. 我不希望任何构造函数调用这些函数。

  3. 有什么办法可以实现这一点,否则这都是垃圾。

c++ oop inheritance

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

如何使用try catch与构造函数?

我看到很多例子,但我无法理解,如何使用try catch与一个简单的构造函数,我写了一个示例程序:

class A
 {
   public:
    try {
       A()
        { cout << "in costr\n"; throw 10;}
    }//try closed
   catch (int a)
{ cout << "caught 1 \n"; }

 };

main()
 {
   A *ptr = new A;
   }
Run Code Online (Sandbox Code Playgroud)
  1. 该程序给出了编译错误
  2. 如果发现异常,对象会发生什么?

c++ constructor try-catch

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

为什么在结构初始化期间使用点运算符?

可能重复:
点(.)在struct初始化程序中的含义是什么?
[N ... M]在C聚合初始化器中的含义是什么?

struct confd_data_cbs    ssd_shdiag_callback = {
    .callpoint  = show_diag__callpointid_diag_cp,
    .get_object = ssd_common_get_object,
    .get_next   = ssd_common_get_next,
};
Run Code Online (Sandbox Code Playgroud)

.callback,.get_object,.get_next?

c struct

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