小编14K*_*14K的帖子

使用.ipp扩展名和.cpp扩展名文件之间的区别

假设我有2个头文件,1个.ipp扩展文件和一个main.cpp文件:

第一个头文件(如Java中的接口):

template<class T>

class myClass1{

public:


    virtual int size() = 0;

};
Run Code Online (Sandbox Code Playgroud)

第二个头文件:

#include "myClass1.h"



    template<class T>

    class myClass2 : public myClass1<T>

     public:



    {

          virtual int size();


     private:

         int numItems;

    };

    #include "myClass2.ipp"
Run Code Online (Sandbox Code Playgroud)

然后是我的myClass2.ipp文件:

template <class T>
int myClass2<T>::size()
{

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

最后一个是我的主要:

#include "myclass2.h"
void tester()
{
  myClass2<int> ForTesting;
  if(ForTesting.size() == 0)
  {
    //......
  } 
  else 
  {
   //.....
  }
}

int main(){

   tester();
   return 0;

}
Run Code Online (Sandbox Code Playgroud)

myClass1,myClass2和myClass2.ipp属于头文件.源文件中的main.cpp.使用这种方式实现程序而不是仅使用.h和.cpp文件有什么好处?什么是.ipp扩展名的文件?.ipp和.cpp之间的区别?

c++

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

释放内存时是否需要使用IF语句?

我试图理解C++中的内存部分.我在使用下面的代码生成输出后尝试释放内存.

题:

是否有必要使用if语句释放内存?

码:

int main(){
    char *pc;
    int *pi;

    pc = new char('a');
    pi = new int(8);

    cout << *pc << endl;
    cout << *pi << endl;

    //What's the purpose for doing if(pc) and if (pi) below?

    if(pc){
        delete pc;
    }
    if(pi){
        delete pi;
    }

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

我可以这样做吗?int main(){char*pc; int*pi;

    pc = new char('a');
    pi = new int(8);

    cout << *pc << endl;
    cout << *pi << endl;


    delete pc;
    delete pi;

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

c++ pointers

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

创建新指针的方法

我试图通过两种方式创建一个新指针,它们的意思是一样的吗?

int* ptn;
int* ptn(nullptr);
Run Code Online (Sandbox Code Playgroud)

c++ pointers c++11

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

标签 统计

c++ ×3

pointers ×2

c++11 ×1