How does one initialize a const struct member of an object? Since it's const, this has to be done in the constructor initialization list. Unfortunately I can't seem to get the syntax right... -
-
class Example
-
{
-
public:
-
Example() : st.a(1), st.b(5) // error
-
{
-
}
-
-
const struct TheStruct
-
{
-
int a;
-
int b;
-
} st;
-
};
-
Obviously it looks like I'm trying to reference nonexistant methods of the struct. What is the correct syntax?
5 13233
How does one initialize a const struct member of an object? Since it's const, this has to be done in the constructor initialization list. Unfortunately I can't seem to get the syntax right... -
-
class Example
-
{
-
public:
-
Example() : st.a(1), st.b(5) // error
-
{
-
}
-
-
const struct TheStruct
-
{
-
int a;
-
int b;
-
} st;
-
};
-
Obviously it looks like I'm trying to reference nonexistant methods of the struct. What is the correct syntax?
Are you trying to declare a const instance of the struct in the class? If so, use "static const." That way, it will be initialized, then it cannot be changed.
something like -
class MyClass
-
{
-
public:
-
MyClass(){};
-
struct MyStruct
-
{
-
static const int a = 1;
-
static const int b = 2;
-
};
-
-
MyStruct MyInstanceOfMyStruct;
-
};
-
Hi Sick0Fant, and thanks for your reply. If I initialized it as you proposed, then it would be the same accross all instances of Example objects. However, by const I merely mean to enforce that, once initialized, the struct should not change. It will, however, hold different values accross different instances of Example. Do you know how I may initialize such a struct in the initialization list of the Example constructor?
Here you go: -
class Example
-
{
-
public:
-
Example() : st(1,5)
-
{
-
}
-
-
struct TheStruct
-
{
-
int a;
-
int b;
-
TheStruct(int first, int second);
-
};
-
const TheStruct st;
-
};
-
Example::TheStruct::TheStruct(int first, int second) : a(first), b(second)
-
{
-
-
}
-
int main()
-
{
-
Example obj;
-
}
-
What you have to do is have a const data member. But that means the const values need to be set before the TheStruct constructor is called so st.a is too late.
What I did was create a TheStruct declaration followed by a const instance of TheStruct. That requires a constructor for the struct.
Here you go: -
class Example
-
{
-
public:
-
Example() : st(1,5)
-
{
-
}
-
-
struct TheStruct
-
{
-
int a;
-
int b;
-
TheStruct(int first, int second);
-
};
-
const TheStruct st;
-
};
-
Example::TheStruct::TheStruct(int first, int second) : a(first), b(second)
-
{
-
-
}
-
int main()
-
{
-
Example obj;
-
}
-
What you have to do is have a const data member. But that means the const values need to be set before the TheStruct constructor is called so st.a is too late.
What I did was create a TheStruct declaration followed by a const instance of TheStruct. That requires a constructor for the struct.
That is so incredibly filthy! No, seriously, very clever WFC; my compliments.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Geiger Ho |
last post by:
Hi all,
Consider:
a.hh
====
struct A {
mytype_t attr1,
mytype_t attr2,
|
by: hilmilho |
last post by:
Hi you,
I'm facing a strange problem, and I don't know what to do. Maybe
someone could help me understand this?
I'm storing IP and Netmask on the struct below.
1st I store IP an print it, its...
|
by: googler |
last post by:
Hello, I have a very simple question that I'm a bit confused about
right now. Is it OK to allocate memory by malloc() or calloc() for a
struct member and then call free() on it? For example, I have...
|
by: bill |
last post by:
I recently realized that I have a structure that I'd like to put more
protection on in the following sense: I'd like to modify
struct foo{
int *x;
} ;
to be:
struct foo {
const int *x;
|
by: geri.gan |
last post by:
I have C API just like this:
enum void getinfor(const struct inputinfor *a, const struct outputinfor
** b)
i use p/invok to translate it to
internal static extern void getinfor(ref...
|
by: Bill Pursell |
last post by:
Suppose I have a structure with many members, and I pass a pointer to
that structure to a function. I'd like the prototype of the function
to specify that it will only be changing certain members...
|
by: sean.scanlon |
last post by:
can someone help understand how i can could access a struct field
dymanically like: foo->fields ?
when i try to compile this i get the following error: 'struct pwd' has
no member named 'fields'...
|
by: myfavdepo |
last post by:
Hi friends,
i am having some trouble in my prog. with struct
member alignment. I have two different static libraries that i use,
each with "struct member alignment" set to 8 bytes. In my...
|
by: petschy |
last post by:
Hello All,
I confronted recently with the fact that the size of a struct member
can't be determined with sizeof(Struct::Member). I'm aware of the
alternative methods to do this, however, I'm...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |