473,670 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 = ThirdPartyAPIFu nction( GetHLSL() );
}

private:

const std::string GetHLSL()
{
std::string hlsl = "";
/*1*/ hlsl += " // This is HLSL code";
/*2*/ hlsl += "float4 IntensityAmbien t;";

return hlsl;
}

ThirdPartyAPIVa ribale * m_effect;
};
Sep 11 '08 #1
6 6670
On 2008-09-10 21:02:24 -0400, Christopher <cp***@austin.r r.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.r r.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.r r.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.no wrote:
* Eric Johnson:
On Sep 10, 6:02 pm, Christopher <cp...@austin.r r.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 "intuitivel y".

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
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.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
"intuitivel y".
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 objektorientier ter Datenverarbeitu ng
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
3557
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 class *Shared* methods which make use of the class ID. So I gave the base class a classID field, and then I gave the derived classes Shared constructors, which I used to set the classID field to the appropriate values for each derived class. But...
10
2241
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 instance constructor. This is described further in Section 10.10.1. So this means:
45
6338
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 parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
14
2712
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
2117
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 it finds a matching "Broom", i want it to return a "Broom" object to the calling program. I want the static method to call the constructor for the "Broom" class. I want this static method, (and one other called "CreateBroom") to be the only...
40
36084
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 object. TIA Sek
26
2136
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); this.property1 = arg; this.property2 = aConstantDefinedGlobally; this.method1 = function (anArg) {
18
3657
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
7230
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
15970
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 creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
0
8471
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8903
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8815
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8661
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6216
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4213
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4393
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.