Can constructor call another method? : c++
This is a discussion on Can constructor call another method? within the c++ forums in Programming Languages category; Its been awhile and I am rusty. Can the constructor of my class call another method in the same class if that other method does not change any member data? I want to simply have a seperate method that returns a huge string, that string containing code in another language, which is to be compiled by a third party API when my object is being contructed. class MyClass() { public: MyClass() { m_effect = ThirdPartyAPIFunction( GetHLSL() ); } private: const std::string GetHLSL() { std::string hlsl = ""; /*1*/ hlsl += " // This is HLSL code"; /*2*/ hlsl += "float4 ...
| c++ comp.lang.c++ usenet archive |
![]() |
| | LinkBack | Thread Tools |
|
#1
| |||
| |||
| Can the constructor of my class call another method in the same class if that other method does not change any member data? I want to simply have a seperate method that returns a huge string, that string containing code in another language, which is to be compiled by a third party API when my object is being contructed. class MyClass() { public: MyClass() { m_effect = ThirdPartyAPIFunction( GetHLSL() ); } private: const std::string GetHLSL() { std::string hlsl = ""; /*1*/ hlsl += " // This is HLSL code"; /*2*/ hlsl += "float4 IntensityAmbient;"; return hlsl; } ThirdPartyAPIVaribale * m_effect; }; |
|
#2
| |||
| |||
| On 2008-09-10 21:02:24 -0400, Christopher <cpisz@austin.rr.com> said: > Can the constructor of my class call another method in the same class > if that other method does not change any member data? Yes. And it can call another member function even if that member function changes the values of data members. In fact, it's pretty common to have an init() member function that does some of the hard work, and gets called from various constructors, assignment operators, etc. -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference (www.petebecker.com/tr1book) |
|
#3
| |||
| |||
| Christopher wrote: > Its been awhile and I am rusty. > > Can the constructor of my class call another method in the same class > if that other method does not change any member data? Yes, it can call another method in the same class. And there is no limit to change member data or not. |
|
#4
| |||
| |||
| On Sep 10, 6:02 pm, Christopher <cp...@austin.rr.com> wrote: > Its been awhile and I am rusty. > > Can the constructor of my class call another method in the same class > if that other method does not change any member data? > One other thing to watch out for -- you want to stay away from calling virtual functions from within a constructor. Search in this list for "Call virtual function in constructor" for more info on that topic. Happy coding! -Eric |
|
#5
| |||
| |||
| * Eric Johnson: > On Sep 10, 6:02 pm, Christopher <cp...@austin.rr.com> wrote: >> Its been awhile and I am rusty. >> >> Can the constructor of my class call another method in the same class >> if that other method does not change any member data? >> > > One other thing to watch out for -- you want to stay away from calling > virtual functions from within a constructor. There's no reason to. In C++ it's safe to call virtual functions from a constructor. Other languages (Java, C#) have problems with that, not C++. Cheers & hth., - Alf -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? |
|
#6
| |||
| |||
| On 2008-09-11 12:37:40 -0400, Eric Johnson <eric.eerriicc@gmail.com> said: > On Sep 10, 6:02Â pm, Christopher <cp...@austin.rr.com> wrote: >> Its been awhile and I am rusty. >> >> Can the constructor of my class call another method in the same class >> if that other method does not change any member data? >> > > One other thing to watch out for -- you want to stay away from calling > virtual functions from within a constructor. Search in this list for > "Call virtual function in constructor" for more info on that topic. > Well, yes, if you don't understand the implications of doing that. But there's nothing inherently dangerous in it. -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference (www.petebecker.com/tr1book) |
|
#7
| |||
| |||
| On Sep 11, 7:15 pm, "Alf P. Steinbach" <al...@start.no> wrote: > * Eric Johnson: > > On Sep 10, 6:02 pm, Christopher <cp...@austin.rr.com> wrote: > >> Its been awhile and I am rusty. > >> Can the constructor of my class call another method in the > >> same class if that other method does not change any member > >> data? > > One other thing to watch out for -- you want to stay away > > from calling virtual functions from within a constructor. > There's no reason to. > In C++ it's safe to call virtual functions from a constructor. > Other languages (Java, C#) have problems with that, not C++. I don't know about C#, but it's "safe" in Java as well. Just safe in a different (IMHO less useful) way. In both cases, you have to know what it means. And for obvious reasons, in neither case can it behave "intuitively". -- James Kanze (GABI Software) email:james.kanze@gmail.com Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 |
|
#8
| |||
| |||
| * James Kanze: > On Sep 11, 7:15 pm, "Alf P. Steinbach" <al...@start.no> wrote: >> * Eric Johnson: > >>> On Sep 10, 6:02 pm, Christopher <cp...@austin.rr.com> wrote: >>>> Its been awhile and I am rusty. > >>>> Can the constructor of my class call another method in the >>>> same class if that other method does not change any member >>>> data? > >>> One other thing to watch out for -- you want to stay away >>> from calling virtual functions from within a constructor. > >> There's no reason to. > >> In C++ it's safe to call virtual functions from a constructor. > >> Other languages (Java, C#) have problems with that, not C++. > > I don't know about C#, but it's "safe" in Java as well. Just > safe in a different (IMHO less useful) way. I don't know what you mean by that. In fact, unless my memory is playing tricks on me, you have earlier remarked on the unsafety of Java in that respect, maintaining that it's a major cause of errors in Java programs. Anyway, virtual calls from constructors are not type safe in Java. > In both cases, you > have to know what it means. I think that goes for any language feature. :-) > And for obvious reasons, in neither > case can it behave "intuitively". Hm, the C++ rules are very intuitive -- unless one's background is from Java. Cheers, - Alf -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? |
|
#9
| |||
| |||
| On Sep 12, 12:23 am, "Alf P. Steinbach" <al...@start.no> wrote: > * James Kanze: [...] > >> In C++ it's safe to call virtual functions from a constructor. > >> Other languages (Java, C#) have problems with that, not C++. > > I don't know about C#, but it's "safe" in Java as well. Just > > safe in a different (IMHO less useful) way. > I don't know what you mean by that. In fact, unless my memory > is playing tricks on me, you have earlier remarked on the > unsafety of Java in that respect, maintaining that it's a > major cause of errors in Java programs. Anyway, virtual calls > from constructors are not type safe in Java. It's safe in the sense that the behavior is well defined, and you can take precautions and handle it. (All of the member variables of the derived class are "zero initialized" before the base class constructor runs.) That doesn't mean that it isn't a major cause of errors; just because you can handle it correctly doesn't mean that programmers think to do so. It's definitely a case of poor language design. (You'll note that I put safe in quotes. To indicate that I was using the word in a very special, and in this case, restricted sense.) > > In both cases, you have to know what it means. > I think that goes for any language feature. :-) Yes. But some are more intuitive than others: I've yet to encounter a language where + meant anything but addition. Where as I can't really think of an intuitive meaning in this case: there's nothing intuitive about what C++ does, and what Java does is even worse. But a language has to define something. > > And for obvious reasons, in neither case can it behave > > "intuitively". > Hm, the C++ rules are very intuitive -- unless one's > background is from Java. I disagree. I don't think that there is any real "intuitive" behavior here. Neither in C++ nor in Java. We're beyond the realm of intuition. -- James Kanze (GABI Software) email:james.kanze@gmail.com Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 |


