小编Luc*_*ore的帖子

如果函数在头文件中定义,则内联关键字是否有意义?

所有代码从我读过的内容来看,A1和A2是相同的,但如果A3与A2完全相同,我不会这样做.我知道代码将编译,因为所有A类都被tmemplated.

注意:所有类和方法声明都在.h文件中.

template <typename _Ty>
class A1 {
public:
    A1();
    void foo() { ... }
};


template <typename _Ty>
class A2 {
public:
    A2();
    void foo();
};

template <typename _Ty>
inline void A2<_Ty>::foo() { ... }


template <typename _Ty>
class A3 {
public:
    A3();
    void foo();
};

template <typename _Ty>
void A3<_Ty>::foo() { ... } // note: No inline keyword here.
Run Code Online (Sandbox Code Playgroud)

PS我在stackoverflow上看过这个问题的变种,但不是这个问题.

c++ templates inline

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

鉴于以下代码(在GCC 4.3中),为什么转换为引用被调用两次?

鉴于以下代码(在GCC 4.3中),为什么在这两种情况下都会调用转换为引用?

class A { };

class B {
public:
  operator A() {}
  operator A&() {}
};

int main() {
  B b;
  (A) b;
  (A&) b;
}
Run Code Online (Sandbox Code Playgroud)

http://ideone.com/d6iF8

c++

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

覆盖父类的功能

class classa {
public:
    virtual void foo();
};

class classb : public classa {
public:
     virtual void foo() override;
};


void classa::foo()
{
    std::cout << "foo from a" << std::endl;
}

void classb::foo()
{
    std::cout << "foo from b" << std::endl;
}

int main()
{
    std::vector<classa> stuff; 

    classa a;
    classb b;

    stuff.push_back(a);
    stuff.push_back(b);

    stuff[0].foo();
    stuff[1].foo();


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

我期望上面的代码返回

foo from a 
foo from b
Run Code Online (Sandbox Code Playgroud)

但它同时返回foo from a.

我认为这是因为矢量存储classa但我不确定.我该如何classb:foo()被召唤b

c++ overriding class function

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

从int16_t转换为uint32_t

int16_t s;
uint32_t ui = s;
Run Code Online (Sandbox Code Playgroud)

int16_t值转换为uint32_t编译器依赖的值的结果是什么?如果没有,那么规则是什么?

c c++

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

为什么抛出vararg函数参数很重要?

最近我遇到了一个函数接受可变数量的参数的问题,并期望最后一个参数是一个空指针.我没有访问它的实现.

将最后一个参数转换为void*有效但直接传入NULL(nullptr不可用)不会:

foo(x,y,(void*)NULL);   //okay
foo(x,y,NULL);          //crash
Run Code Online (Sandbox Code Playgroud)

国际海事组织这不应该有所作为,但话说回来,我以前错了.你能想出演员会有什么不同的原因吗?或者这只是一个意外(一些不连续或错误的构建或smth.沿着这些线)

提前抱歉,我无法提供更多详细信息.

c++ casting variadic-functions

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

对象A是B的成员,对象B是A的成员

我有一个c ++,在A和B类中,每个人都有另一个类的成员.我怎么解决这个问题?

class A {
     B memberY;
}


class B {
     A memberX;
}
Run Code Online (Sandbox Code Playgroud)

c++ oop

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

来自`cmd`文件的进程的CreateProcess

我正在尝试从不同的流程启动流程.实现这一目标的机制不会发生变化.发射器和原始过程都位于C:\dir.

我正在从cmd文件中启动我的启动器.该cmd文件本身位于其他地方,为了为它找到了启动程序可执行文件,我设置的PATH变量:

set PATH=C:\dir;%PATH%;
launcher.exe
Run Code Online (Sandbox Code Playgroud)

启动程序使用以下代码启动子进程:

  STARTUPINFO startupInfo;
  startupInfo.cb               = sizeof (STARTUPINFO);
  startupInfo.lpReserved       = 0;
  startupInfo.lpDesktop        = NULL;
  startupInfo.lpTitle          = NULL;
  startupInfo.dwX              = 0;
  startupInfo.dwY              = 0;
  startupInfo.dwXSize          = 0;
  startupInfo.dwYSize          = 0;
  startupInfo.dwXCountChars    = 0;
  startupInfo.dwYCountChars    = 0;
  startupInfo.dwFillAttribute  = 0;
  startupInfo.dwFlags          = _showInForeground ? STARTF_USESHOWWINDOW : 0;
  startupInfo.wShowWindow      = _showInForeground ? 1 : 0;
  startupInfo.cbReserved2      = 0;
  startupInfo.lpReserved2      = 0;

  PROCESS_INFORMATION processInfo;

  BOOL retVal = CreateProcess("child.exe", "", NULL, NULL, FALSE, …
Run Code Online (Sandbox Code Playgroud)

c++ winapi visual-c++

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

C++命名空间和多个定义的变量

我有两个C++命名空间,如下所示:

#ifndef TRANS_H
#define TRANS_H
namespace Trans
{
   double Delta[3];
   double calcDeltaPositions();
   //and more that I will leave out for simplicity
};
#endif

#ifndef SPACE_H
#define SPACE_H
namespace Space
{
   double vels[3];
   void calcAccel(double DeltaVal[3]);
};
#endif
Run Code Online (Sandbox Code Playgroud)

现在我有一个main.cpp文件:

#include "Trans.h"
#include "Space.h"
int main()
{
    double pos = Trans::calcDeltaPositions();
    Space::calcAccel(Trans::Delta);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我一直得到一个错误,声称Delta是main.o和Trans.o中的多重定义.这怎么可能是因为我只声明Delta存在于Trans中?

c++ namespaces definition

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

为什么MSVC不为cout或const char*优化cout,但它对int有效?

比较代码:

    const char x = 'a';
    std::cout<< x;
00C31000  mov         eax,dword ptr [__imp_std::cout (0C32054h)]  
00C31005  push        eax  
00C31006  call        std::operator<<<std::char_traits<char> > (0C310B0h)  
00C3100B  add         esp,4  
Run Code Online (Sandbox Code Playgroud)

    const int x = 'a';
    std::cout<< x;
00271000  mov         ecx,dword ptr [__imp_std::cout (272048h)]  
00271006  push        61h  
00271008  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (272044h)]  
Run Code Online (Sandbox Code Playgroud)

    const char* x = "a";
    std::cout<< x;
00071000  mov         eax,dword ptr [__imp_std::cout (72058h)]  
00071005  push        eax  
00071006  call        std::operator<<<std::char_traits<char> > (710B0h)  
0007100B  add         esp,4 
Run Code Online (Sandbox Code Playgroud)

似乎该const int版本比(const char* …

c++ compiler-construction optimization

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

视觉工作室表达抱怨失踪';' 输入c程序后

我的代码有什么问题?

#include<stdio.h>
#include<string.h>

int main() {
    FILE *file;

    char string[32] = "Teste de solução";

    file = fopen("C:\file.txt", "w");

    printf("Digite um texto para gravar no arquivo: ");
    for(int i = 0; i < 32; i++) {
        putc(string[i], file);
    }

    fclose(file);

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

错误:

c:\users\guilherme\documents\visual studio 2010\projects\helloworld\helloworld\hello.c(13): error C2143: syntax error : missing ';' before 'type'
1>c:\users\guilherme\documents\visual studio 2010\projects\helloworld\helloworld\hello.c(13): error C2143: syntax error : missing ';' before 'type'
1>c:\users\guilherme\documents\visual studio 2010\projects\helloworld\helloworld\hello.c(13): error C2143: syntax error : missing ')' before 'type'
1>c:\users\guilherme\documents\visual …
Run Code Online (Sandbox Code Playgroud)

c

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