使用私有成员初始化静态常量、非整数、类成员

发布于 2024-12-28 15:58:23 字数 1231 浏览 3 评论 0原文

我正在尝试初始化一个静态常量非整数。但是它需要私有参数。如果它是整型类型,您可以将其放置在类主体中,并允许一个变量取另一个变量的值,即

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪漫之都 2025-01-04 15:58:23

将此公共函数添加到您的 Person 类中。

static const sf::Shape defaultShape();

此函数既可以访问私有变量,也可以初始化静态 Shape 变量。

Add this public function to your Person class.

static const sf::Shape defaultShape();

This function can both access the private variables and initialize your static Shape variable.

独木成林 2025-01-04 15:58:23

不要使用类中的静态值来计算其他静态值。在这种情况下,如果您保持正确的顺序(静态变量初始化顺序),这是可能的)

你的课程设计很可疑。如果 x,y,rad,col 仅在圆中使用,则最好只初始化默认圆并且根本不使用这些变量。

改成

static const float x ;

static float x() { return 0 ; }

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

static const float x ;

into

static float x() { return 0 ; }

etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文