473,396 Members | 2,087 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.

Can constructor call another method?

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 IntensityAmbient;";

return hlsl;
}

ThirdPartyAPIVaribale * m_effect;
};
Sep 11 '08 #1
6 6649
On 2008-09-10 21:02:24 -0400, Christopher <cp***@austin.rr.comsaid:
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)

Sep 11 '08 #2
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.
Sep 11 '08 #3
On Sep 10, 6:02*pm, Christopher <cp...@austin.rr.comwrote:
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
Sep 11 '08 #4
On 2008-09-11 12:37:40 -0400, Eric Johnson <er***********@gmail.comsaid:
On Sep 10, 6:02Â*pm, Christopher <cp...@austin.rr.comwrote:
>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)

Sep 11 '08 #5
On Sep 11, 7:15 pm, "Alf P. Steinbach" <al...@start.nowrote:
* Eric Johnson:
On Sep 10, 6:02 pm, Christopher <cp...@austin.rr.comwrote:
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:ja*********@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
Sep 11 '08 #6
On Sep 12, 12:23 am, "Alf P. Steinbach" <al...@start.nowrote:
* 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:ja*********@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
Sep 12 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: John Brock | last post by:
I have a base class with several derived classes (I'm writing in VB.NET). I want each derived class to have a unique class ID (a String), and I want the derived classes to inherit from the base...
10
by: linkspeed | last post by:
Following texts are from C# spec. The optional constructor-initializer specifies another instance constructor to invoke before executing the statements given in the constructor-body of this...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
14
by: Arne | last post by:
In C++ we have a copy constructor. What is the equivalent in .Net? Would that be a clone method?
5
by: ffrugone | last post by:
My scenario involves two classes and a database. I have the classes "Broom" and "Closet". I want to use a static method from the "Closet" class to search the database for a matching "Broom". If...
40
by: Sek | last post by:
Is it appropriate to throw exception from a constructor? Thats the only way i could think of to denote the failure of constructor, wherein i am invoking couple of other classes to initialise the...
26
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
18
by: AlexanderVX | last post by:
How do I write a constructor mehtod call in this case /*-----------*/ template<typename Tclass CObjectPoolImpl { public: void smth(T* pObj) { if (pObj)
23
by: TarheelsFan | last post by:
What happens whenever you throw an exception from within a constructor? Does the object just not get instantiated? Thanks for replies.
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.