• Post Reply Bookmark Topic Watch Topic
  • New Topic

Coding convention for Class constructors  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

So I have a class which has 2 other programmer defined classes in it as private data members. (The classes have separate files)

In c++ I read from various sources that doing any work(like calling another constructor) in a constructor is not a good idea because of compilation at run time and some code not being compiled yet. (Something to that affect)

I was wondering when the best time to initialize these data members that are programmer defined classes would be, if not in the constructor? (Maybe that issues does not apply to Java but, I think it would)

I ask because the constructor of the top level class will take parameters that are needed to initialize these data members.

Thank you!

This is my first time ever posting in a forum haha
Feels empowering!

-philFaster
 
Bartender
Posts: 572
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you should create the instance parameters in your constructor. For example, you might have:
 
Philip Fuster
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:Yes, you should create the instance parameters in your constructor. For example, you might have:



Thank you!
A follow up question here.
For all data members that are not primitives I must use the new operator to initialize them before use outside of the class and in a top lvl class that runs the program correct?
 
Fred Kleinschmidt
Bartender
Posts: 572
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For all data members that are not primitives I must use the new operator to initialize them before use outside of the class and in a top lvl class that runs the program correct?

Not necessarily.  You might have data members that are set to static instances of another class (For example, you might have a PrintStream that you set either to System.out or to a new PrintStream, depending on inputs to your constructor), or you might initialize a variable using a factory method of some class (which itself uses the "new" operator, but that is hidden from you), or you might clone an object that is passed as a parameter to your method, or just set it equal to a parameter passed in.
 
Marshal
Posts: 56610
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Philip Fuster wrote:. . . For all data members that are not primitives I must use the new operator to initialize them before use outside of the class and in a top lvl class that runs the program correct?
No. As well as what FK said: some objects are instantiated without using new at all; the best‑known are String literals.
What does lvl mean? Please read this FAQ. Such abbreviations can seriously confuse non‑English‑speakers who rely on translation tools.
The term top‑level class has nothing to do with the class having a main method or not. It means the class is not inside another class.
If you are writing Java®, beware of what you learnt in C++. The two languages are different and things which appear to be the same can behave differently.

...and welcome to the Ranch
 
Philip Fuster
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
What does lvl mean? Please read this FAQ. Such abbreviations can seriously confuse non‑English‑speakers who rely on translation tools.
The term top‑level class has nothing to do with the class having a main method or not. It means the class is not inside another class.
If you are writing Java®, beware of what you learnt in C++. The two languages are different and things which appear to be the same can behave differently.


Thank you!

I will be sure to keep everything you have stated in mind!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!