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

Initialization list

My companies C++ coding standard states that "Member initialization list should be used instead of assignment". I have a datamember that has to be initialized through a function call. Can I have function calls in initialization list.
Jan 29 '08 #1
2 1581
I too had the same doubt. However I tried it out and it seemed to work fine. I made the following observations though. The order of execution of the function is not defined. This is compiler dependent. Next thing is that the function should generally not be a instance member function, since we cannot count on the object to be completely initialized.

Consider the following code:

Expand|Select|Wrap|Line Numbers
  1. class TestClass
  2. {
  3. public:
  4.     TestClass();
  5.     TestClass(int a, int b);
  6.     TestClass(int a, int b, int c);
  7.  
  8. private:
  9.     int a,b,c;
  10.     int instanceGet()
  11.     {
  12.         return a+b;
  13.     }
  14. };
  15.  
  16. int getFunction()
  17. {
  18.     return 1;
  19. }
  20.  
  21. TestClass::TestClass(void)
  22. :a(0),b(1),c(a+b)
  23. {
  24.     std::cout<<"a: "<<a<<" b: "<<b<<" c: "<<c<<std::endl;
  25. }
  26.  
  27.  
  28. TestClass::TestClass(int a, int b)
  29. :a(a),b(b),c(getFunction())
  30. {
  31.     std::cout<<"a: "<<a<<" b: "<<b<<" c: "<<c<<std::endl;
  32. }
  33.  
  34. TestClass::TestClass(int a, int b, int c)
  35. :a(a),c(instanceGet()),b(b)
  36. {
  37.     std::cout<<"a: "<<a<<" b: "<<b<<" c: "<<c<<std::endl;
  38.  
  39. }
  40.  
  41. int main()
  42. {
  43.     TestClass c1;
  44.     TestClass c2(10,20);
  45.     TestClass c3(10,20,30);
  46.     return 0;
  47. }
  48.  
The order of evaluation of parameters is left to implementation. However the purpose of using initialization list is to directly give the value to a variable instead of doing an assignment. So calling a function in initialization list might not be so efficient
Jan 29 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
The order of execution of the function is not defined. This is compiler dependent.
Not true.

Member variables are initialized in the order they appear in the class declaration and not in the order of the initialization list.
Jan 29 '08 #3

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

Similar topics

1
by: Jacek Dziedzic | last post by:
Hi! A) Why isn't it possible to set a member of the BASE class in an initialization list of a DERIVED class constructor (except for 'calling' the base constructor from there, of course)? I even...
4
by: Anton Pervukhin | last post by:
Hi everybody! I have a small problem regarding the initialization of pointer to the file stream under some conditions. Imagine a class which has a pointer to output file stream and some...
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
4
by: Jacek Dziedzic | last post by:
Hello! Suppose I have a class Foo that defines a default c'tor that initializes some data using an initialization list: Foo::Foo() : member1(0), member2(0), member3(NULL), member4(20) // and...
6
by: anongroupaccount | last post by:
class CustomType { public: CustomType(){_i = 0;} CustomType(int i) : _i(i) {} private: int _i; }; class MyClass
5
by: toton | last post by:
Hi, I can initialize an array of class with a specific class as, class Test{ public: Test(int){} }; Test x = {Test(3),Test(6)}; using array initialization list. (Note Test do NOT have a...
15
by: jamx | last post by:
How can you initialize an array, in the initialization list of a constructor ?? SomeClass { public: SomeClass() : *init here* { } private: int some_array; };
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: 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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.