473,327 Members | 1,979 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,327 software developers and data experts.

relation between access specifiers and using initializer lists for POD types in c++0x

8
take two following classes:

Expand|Select|Wrap|Line Numbers
  1. class Test1{
  2.  public:
  3.   Test1()=default;
  4.   Test1(char in1,char in2):char1(in1),char2(in2){}
  5.   char char1;
  6.   char char2;
  7. };
  8. class Test2{
  9.  public:
  10.   Test2()=default;
  11.   Test2(char in1,char in2):char1(in1),char2(in2){}
  12.  private:
  13.   char char1;
  14.   char char2;
  15. };
I know in c++0x both of these classes are considered as POD types and we can initialize objects of them using initializer lists as below:

Expand|Select|Wrap|Line Numbers
  1. Test1 obj1={'a','b'};//valid in c++0x
  2. Test2 obj2={'a','b'};//valid in c++0x
But I wonder what the technical reason is that when we have different access specifiers in a class like below, it's not possible to use initializer list for initializing objects of that class and that class is not considered as a POD type ?

Expand|Select|Wrap|Line Numbers
  1. class Test{
  2.  public:
  3.   Test()=default;
  4.   Test(char in1,char in2):char1(in1),char2(in2){}
  5.   char char1;
  6.  private:
  7.   char char2;
  8. };
  9. Test obj={'a','b'};//invalid in c++0x
  10.  
In case you don't know definition of PODs in c++0x:
A class/struct is considered a POD if it is trivial, standard-layout, and if all of its non-static members are PODs.

A trivial class or struct is defined as one that:

1. Has a trivial default constructor. This may use the default constructor syntax (SomeConstructor() = default;).
2. Has a trivial copy constructor, which may use the default syntax.
3. Has a trivial copy assignment operator, which may use the default syntax.
4. Has a trivial destructor, which must not be virtual.

A standard-layout class or struct is defined as one that:

1. Has only non-static data members that are of standard-layout type
2. Has the same access control (public, private, protected) for all non-static members
3. Has no virtual functions
4. Has no virtual base classes
5. Has only base classes that are of standard-layout type
6. Has no base classes of the same type as the first defined non-static member
7. Either has no base classes with non-static members, or has no non-static data members in the most derived class and at most one base class with non-static members. In essence, there may be only one class in this class's hierarchy that has non-static members.

In case you don't know what a trivial constructor or operator is:
Compiler generates a trivial one of each of following items for a class, in case it isn't user-declared: Copy constructor, destructor and copy assignment operator.
And also if there's no user-declared constructor for a class, a trivial default constructor is generated for that class, in case there are any user-declared constructors you can use the syntax(SomeConstructor() = default;) to make your own trivial default constructor.
Jul 21 '10 #1
0 1191

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

Similar topics

5
by: Andy Skypeck | last post by:
I am looking for some validation against a dubious coding practice that prevails where I work. C types defined in types.h (Linux) or stdint.h (Windows, C99?) are used as if they belong to the C++...
30
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g =...
2
by: ssunderk | last post by:
In my program, I should be able to access URL using the code in VC++/VB. I would appreciate if anybody can provide a sample code. -- ssunderk...
15
by: Claudio Grondi | last post by:
Let's consider a test source code given at the very end of this posting. The question is if Python allows somehow access to the bytes of the representation of a long integer or integer in...
24
Coldfire
by: Coldfire | last post by:
hello ppl. I am havin this minor problem that i have not yet figured out lately. I am developing a windows application in VStudio.net 2005 that interacts with an Access DB (with latest MS Jet...
16
by: anon.asdf | last post by:
Hi! On a machine of *given architecture* (in terms of endianness etc.), I want to access the individual bytes of a long (*once-off*) as fast as possible. Is version A, version B, or version...
9
by: Ben Rudiak-Gould | last post by:
Background: I have some structs containing std::strings and std::vectors of other structs containing std::strings and std::vectors of .... I'd like to make a std::vector of these. Unfortunately the...
2
by: Ioannis Vranos | last post by:
Based on a discussion about Unicode in clc++ inside a discussion thread with subject "next ISO C++ standard", and the data provided in http://en.wikipedia.org/wiki/C%2B%2B0x , and with the design...
22
by: phil.pellouchoud | last post by:
I did some searching online and i couldn't find anything in reference to this. I am using MinGW, gcc 4.3 and am having the following compilation issue: class CFoo { public: ...
4
by: Alex Vinokur | last post by:
Here is some tuple (triple in this case) with uniform types (for instance, double): boost::tuple<double, double, doublet; Is there any way (in boost::tuple) to define such tuples something like...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.