小编Ale*_*lex的帖子

你如何在子类中重载模板化函数(专用)?

我有一个带有模板化函数的基类,它具有一般模板类型,以及专用版本.

#ifndef BASE_CLASS
#define BASE_CLASS

#include <iostream>

using namespace std;

struct Type1
{
};

struct Type2
{
};

class baseClass
{
    public:
    template<class Type>
    void doStuff(Type & t)
        {
        templateFunction(t);  
        }

    template<class Type>
    void templateFunction(Type & t);
};

template<class Type>
void baseClass::templateFunction(Type & t)
{
    cout << "This is the generic function!" << endl;
}

template<>
void baseClass::templateFunction(Type1 & t)
{
    cout << "This is the specialized function: - Type1" << endl;
}
#endif
Run Code Online (Sandbox Code Playgroud)

我还有一个继承自"baseClass"的子类.但是,子类需要不同的专业化功能.

#ifndef CHILD_CLASS
#define CHILD_CLASS

#include …
Run Code Online (Sandbox Code Playgroud)

c++ templates

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

C/C++中的对象/结构对齐

#include <iostream>

using namespace std;

struct test
{
    int i;
    double h;
    int j;
};

int main()
{
    test te;
    te.i = 5;
    te.h = 6.5;
    te.j = 10;

    cout << "size of an int: " << sizeof(int) << endl; // Should be 4
    cout << "size of a double: " << sizeof(double) << endl; //Should be 8
    cout << "size of test: " << sizeof(test) << endl; // Should be 24 (word size of 8 for double)

    //These two …
Run Code Online (Sandbox Code Playgroud)

c++ g++

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

抑制G ++ 3.4.6中的链接错误

不要问为什么,但有没有办法抑制失败的链接错误?

如:

未定义的BLANK参考

这是在GCC 3.4.6中

c++ linker gcc g++

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

为什么c_str()会打印两次字符串?

所以...当我去的时候:

cout<<stringName<<endl;
Run Code Online (Sandbox Code Playgroud)

我明白了:

NT
Run Code Online (Sandbox Code Playgroud)

但是当我走的时候:

cout<<stringName.c_str()<<endl;
Run Code Online (Sandbox Code Playgroud)

我明白了:

NTNT
Run Code Online (Sandbox Code Playgroud)

为什么?

c++ c-strings

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

标签 统计

c++ ×4

g++ ×2

c-strings ×1

gcc ×1

linker ×1

templates ×1