第二个类不是标准布局的原因是什么?(Visual Studio C++)
#include <iostream>
#include <type_traits>
struct A
{
int i;
};
struct B : public A
{
};
std::cout << "is_standard_layout<B> == "
<< std::boolalpha
<< std::is_standard_layout<B>::value /
ISO C++ (11)是否允许对私有的非静态类成员变量进行优化?这可能会被检测到:
class X { int x; };
assert (sizeof(X) >= sizeof(int));
但我不知道有什么条款要求上面的断言。
为了澄清:(a) C++标准中是否有条款确保上述断言。
(b)有没有人能想到其他方法来检测x的省略?offsetof?
(c)尽管存在(a)和(b)项,但是否允许进行优化?
我有一种感觉,如果类是函数的本地类,那么优化是可能的,但不是这样的(但我希望有明确的引用)。
如果我公开地在基类中typedef某些类型,那么C++标准是否保证typedefed类型对于派生类总是可见的?
例如:
struct A
{
typedef int T;
};
struct B : A
{
void f(T) {}; // Does the C++ standard guarantee it can be compiled?
};
int main()
{
B().f(8);
}
在c#中,静态成员对于每个泛型类都是唯一的,如下例所示
using System;
//A generic class
public class GenTest<T>
{
//A static variable - will be created for each type on refraction
static CountedInstances OnePerType = new CountedInstances();
//a data member
private T mT;
//simple constructor
public GenTest(
我已经学习了C++,现在我正在学习Java。在C++中,我们可以在非静态/实例成员函数中使用静态成员。但是在学习Java的过程中,我发现了这个:
我写了一个程序来检查“非静态函数是否可以访问静态成员”。并且在下面的程序中没有问题:
public class Animal
{
static int CountOfAnimal;
public Animal()
{
CountOfAnimal++;
}
public int showCount()
{
CountOfAnimal++;
retu
在下面的C++程序中,从函数修改静态数据成员const可以正常工作:
class A
{
public:
static int a; // static data member
void set() const
{
a = 10;
}
};
但是,从const函数修改非静态数据成员不起作用:
class A
{
public:
int a; // non-static data member
void set() const
{
a = 10;
}
};
为什么const
我用的是GCC 4.8.2:
$ g++ --version
g++ (GCC) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
我有这样的定义:
#pragma once
#in
我是C++的新手。我经常在一些网站上看到这个变种成员?
class School
{
int x; -> data member.
}
我认识数据会员。但是什么是变体成员呢?
注意:来自c++规范:在构造器页面下。
X is a union-like class that has a variant member with a non-trivial default constructor.
我开始学习C++和QT,但有时我从一本书中粘贴的最简单的代码会导致错误。 我在用g++4.4.2在Ubuntu 10.04上使用QtCreatorIDE。g++编译器语法和其他编译器之间有什么区别吗?例如,当我试图访问静态成员时,总会出现问题。 #include <iostream>
using namespace std;
class A
{
public:
static int x;
static int getX() {return x;}
};
int main()
{
int A::x = 100; // error: invalid
在研究C++时,我想到了一个问题。它是关于静态函数的。在这个头文件中,我创建了一个静态函数。
using namespace std;
class Date
{
public:
int dd, mm, yy;
public:
//Date();
Date(int d, int m, int y);
static bool LeapYear(Date t);
};
在这个源文件中,我定义了函数
{
if ((t.yy % 400 == 0) || (t.yy % 4 == 0) && (t.yy % 100 != 0))
r
我试图定义如下所示的公共静态变量:
public :
static int j=0; //or any other value too
我在这一行上得到了一个编译错误: ISO C++禁止非const静态成员‘J’的类初始化。
为什么C++不允许这样做?
为什么允许初始化const成员?
这是否意味着C++中的静态变量没有像C那样用0初始化?
谢谢!
我刚刚发现了一些C++代码(在,它使用了一种我以前从未见过的技术将类型添加到现有的类中:
class Testpm {
public:
void m_func1() { cout << "m_func1\n"; }
int m_num;
};
// Define derived types pmfn and pmd.
// These types are pointers to members m_func1() and m_num, respectively.
void (Testpm::*pmfn)() = &Testpm::m_func1
我想知道是否可以在子类中将成员附加到C++联合中。
class A {
...
union { int a; int b; };
};
class B : public A {
...
int c; //<- Can this use the same storage as the union?
};
一个更具体的例子是标记联合的概念,您希望有一个子类来向联合添加一个类型。
我有这样的代码:
class A
{
public:
unsigned long a;
static const unsigned long b = sizeof(a); // "error C2327: 'A::a' : is not a type name, static, or enumerator" in VC++
};
我在VC++中有编译器错误,在IAR中没有错误。哪个编译器是对的,C++标准是怎么说的?
我为这个问题的新鲜感提前道歉;我想我没有掌握Actionscript的基础知识,但在其他地方找不到答案。
我想要的是一些全局常量,就像在C++中一样。在C++中,我只需要在一个文件中定义MAP_HEIGHT 20,或者类似的东西,当我在顶部包含该文件时,它们将是全局可访问的。在Actionscript中,我尝试创建一个静态类,如下所示:
package
{
public class Settings {
public const mapx:int = 20;
public function Settings() {}
}
}
但是当我尝试用冒号语法在代码中引用它时
v
对于上下文,我是一个全新的C++新手,我用Java语言编程。所以我的C++语法很差,需要愚蠢的回答。
我似乎不明白为什么我会得到这个错误。我检查了多个答案,但它们都是针对原始变量的。我正在使用对象,我猜这可能会导致一些错误,或者我只是看不见。
这是我的类,重点放在public下的3个静态变量上
class SuperMarket
{
private:
int count;
int totalService;
int totalWait;
public:
static CustomerQ * regularLine;