473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Initializing const struct member

424 256MB
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...
Expand|Select|Wrap|Line Numbers
  1.  
  2. class Example
  3. {
  4. public:
  5.   Example() : st.a(1), st.b(5) // error
  6.   {
  7.   }
  8.  
  9.   const struct TheStruct 
  10.   { 
  11.     int a;
  12.     int b;
  13.   } st;
  14. };
  15.  
Obviously it looks like I'm trying to reference nonexistant methods of the struct. What is the correct syntax?
May 9 '08 #1
5 13233
Sick0Fant
121 100+
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...
Expand|Select|Wrap|Line Numbers
  1.  
  2. class Example
  3. {
  4. public:
  5.   Example() : st.a(1), st.b(5) // error
  6.   {
  7.   }
  8.  
  9.   const struct TheStruct 
  10.   { 
  11.     int a;
  12.     int b;
  13.   } st;
  14. };
  15.  
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
Expand|Select|Wrap|Line Numbers
  1. class MyClass
  2. {
  3.     public:
  4.         MyClass(){};
  5.     struct MyStruct
  6.     {
  7.         static const int a = 1;
  8.         static const int b = 2;
  9.     };
  10.  
  11.     MyStruct MyInstanceOfMyStruct;
  12. };
  13.  
May 9 '08 #2
arnaudk
424 256MB
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?
May 10 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
Here you go:
Expand|Select|Wrap|Line Numbers
  1. class Example
  2. {
  3. public:
  4.   Example() : st(1,5)
  5.   {
  6.   }
  7.  
  8.   struct TheStruct 
  9.   { 
  10.     int a;
  11.     int b;
  12.    TheStruct(int first, int second);
  13.   };
  14.   const TheStruct st;
  15. };
  16. Example::TheStruct::TheStruct(int first, int second) : a(first), b(second)
  17. {
  18.  
  19. }
  20. int main()
  21. {
  22.    Example obj;
  23. }
  24.  
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.
May 10 '08 #4
arnaudk
424 256MB
Great! Thanks agian.
May 12 '08 #5
JosAH
11,448 Expert 8TB
Here you go:
Expand|Select|Wrap|Line Numbers
  1. class Example
  2. {
  3. public:
  4.   Example() : st(1,5)
  5.   {
  6.   }
  7.  
  8.   struct TheStruct 
  9.   { 
  10.     int a;
  11.     int b;
  12.    TheStruct(int first, int second);
  13.   };
  14.   const TheStruct st;
  15. };
  16. Example::TheStruct::TheStruct(int first, int second) : a(first), b(second)
  17. {
  18.  
  19. }
  20. int main()
  21. {
  22.    Example obj;
  23. }
  24.  
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
May 12 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Geiger Ho | last post by:
Hi all, Consider: a.hh ==== struct A { mytype_t attr1, mytype_t attr2,
2
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...
10
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...
6
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;
1
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...
5
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...
9
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'...
4
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...
5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
jinu1996
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...
0
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.