使用私有成员初始化静态常量、非整数、类成员
我正在尝试初始化一个静态常量非整数。但是它需要私有参数。如果它是整型类型,您可以将其放置在类主体中,并允许一个变量取另一个变量的值,即
static const int A=0;
static const int B=A;
但由于它是非整型,因此必须在类主体之外初始化,但由于成员是私有的,因此它们是私有的超出范围,超出类体。
例如
//HEADER
class Person
{
static const float x;
static const float y;
static const int rad;
static const sf::Color col;
static const sf::Shape shape;
};
//CPP
const float Person::x=0;
const float Person::y=0;
const int Person::rad=16;
const sf::Color Person::col(255,0,0,255);
const sf::Shape shape=sf::Shape::Circle(Person::x,Person::y,Person::rad,Person::col);
Person::x,Person::y,Person::rad,Person::col 超出范围,因为它们是私有的。 当我初始化一个静态常量时,我不希望将其放入构造函数中,每次创建新实例时都会调用该构造函数。
例如,
//HEADER
class Person
{
static const float x;
static const float y;
static const int rad;
static const sf::Color col;
static const sf::Shape shape;
Person();
};
//CPP
const float Person::x=0;
const float Person::y=0;
const int Person::rad=16;
const sf::Color Person::col(255,0,0,255);
Person::Person()
{
const sf::Shape shape=sf::Shape::Circle(x,y,rad,col);
}
上面似乎有效,但由于上述原因我不希望使用它。
有解决方法吗?不公开成员。
谢谢
Im am attempting to initialise a static const non-integral. However it requires parameters which are private. If it was of integral type you could place it in the class body and allow allow one variable to take the value of another ie
static const int A=0;
static const int B=A;
But as its non-integral it must be initialised outside of the class body, but as the members are private they are out of scope, outside the class body.
For example
//HEADER
class Person
{
static const float x;
static const float y;
static const int rad;
static const sf::Color col;
static const sf::Shape shape;
};
//CPP
const float Person::x=0;
const float Person::y=0;
const int Person::rad=16;
const sf::Color Person::col(255,0,0,255);
const sf::Shape shape=sf::Shape::Circle(Person::x,Person::y,Person::rad,Person::col);
Person::x,Person::y,Person::rad,Person::col is out of scope as they are private.
As I am initialising a static const I would wish not to put it in the constructor which would be called everytime a new instance is created.
For example
//HEADER
class Person
{
static const float x;
static const float y;
static const int rad;
static const sf::Color col;
static const sf::Shape shape;
Person();
};
//CPP
const float Person::x=0;
const float Person::y=0;
const int Person::rad=16;
const sf::Color Person::col(255,0,0,255);
Person::Person()
{
const sf::Shape shape=sf::Shape::Circle(x,y,rad,col);
}
The above seems to work however I wish not to use it for the above reason.
Is there a work around. Without making the members public.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将此公共函数添加到您的
Person
类中。此函数既可以访问私有变量,也可以初始化静态
Shape
变量。Add this public function to your
Person
class.This function can both access the private variables and initialize your static
Shape
variable.不要使用类中的静态值来计算其他静态值。在这种情况下,如果您保持正确的顺序(静态变量初始化顺序),这是可能的)
你的课程设计很可疑。如果 x,y,rad,col 仅在圆中使用,则最好只初始化默认圆并且根本不使用这些变量。
改成
等
Do not use static value from the class to calculate other static values. In this case it is possible, if you keep things in the right order ( Static variables initialisation order )
The design of your classes are suspicious. If x,y,rad,col are only used in circle, it may be better to just initialize your default circle and not have those variables at all.
change
into
etc.