小编Cha*_*via的帖子

在C++中使用Typcasting vector <object>?

我有两个类,障碍和boid,boid继承自障碍.

现在我想编写一些可以处理这两个类的对象的函数,这样传递vector<boid>就可以了vector<obstacle>.

当我像这样进行类型转换并尝试访问向量的大小时,我得到的数字是1840700394而不是60:

vector<boid>* boids; ....
cout << ((vector<obstacle>*)boids)->size() << endl;
Run Code Online (Sandbox Code Playgroud)

我也试过"reinterpret_cast"但同样的问题.

c++ inheritance casting

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

if(memcmp(版本,"\ x0\x0\x0",3)== 0)

我正在编写一段代码,其中包含如下声明:

if (memcmp (version, "\x0\x0\x0", 3) == 0 )       
Run Code Online (Sandbox Code Playgroud)

与版本进行比较的数据是什么?是否与"000"相比?

c

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

计算文本文件中的字母

可以告诉我这个代码有什么不对吗?它编译和一切都很好,但输出是固定的零一直下降.所以它不算数字.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const char FileName[] = "c:/test.txt";

int main () 
{
    string lineBuffer;
    ifstream inMyStream (FileName); //open my file stream


    if (inMyStream.is_open()) 
    {
       //create an array to hold the letter counts
       int upperCaseCount[26] = {0};
           int lowerCaseCount[26] = {0};

       //read the text file
       while (!inMyStream.eof() )
       {
           //get a line of text
           getline (inMyStream, lineBuffer);
           //read through each letter in the lineBuffer
           char oneLetter;
           for( int n=0; n < (int)lineBuffer.length(); ++n ) …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
2
解决办法
7119
查看次数

如何专门化这个模板(注意返回类型)

我想专注于以下模板:

template <typename T, int P>
T item(size_t s);
Run Code Online (Sandbox Code Playgroud)

这样的事情:

template<int P>
T item<typename T>(size_t s)
{
//for all numeric types
    return static_cast<T>(rand());
}

template <int P>
string item<string>(size_t s)
{
    return "asdf";
}
Run Code Online (Sandbox Code Playgroud)

c++ templates metaprogramming

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

标签 统计

c++ ×3

c ×1

casting ×1

inheritance ×1

metaprogramming ×1

templates ×1