473,804 Members | 3,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

i need advice

i have the next source code:

class myClass
{
public:
myClass(ostream &out=cout) : m_out(out) { }
myClass(string &filename) :
_out(new ofstream(filena me.c_str())),
m_out(*_out) { }

write(cons string &text) { m_out << text; }
protected:
ostream &m_out;
auto_ptr<ostrea m> _out;
}

this is good (and nice). i can make a objects of this class like:
ofstream ofs("test");
myClass mclass(ofs);
or:
myClass mclass();
or:
myClass mclass("test");
and all will be good while i do not decide to make new member function
like this changeFile(stri ng &newfilename ), then i can write the next
code:

void changeFile(stri ng &newfilename )
{
_out = auto_ptr<ofstre am>(new ofstream(newfil ename));
// now _out is ok
m_out = *_out;
// this is not valid; the reference to object can't be changed
// in write function i will be need make checks and run *_out << text
// i don't like this variant
}

are you can help me?
10x in advance :)

Jul 23 '05 #1
4 1465
vo**@softhome.n et wrote:
i have the next source code:

class myClass
{
public:
myClass(ostream &out=cout) : m_out(out) { }
myClass(string &filename) :
_out(new ofstream(filena me.c_str())),
m_out(*_out) { }

write(cons string &text) { m_out << text; }
protected:
ostream &m_out;
auto_ptr<ostrea m> _out;
}

this is good (and nice).
It may be nice, but it's definitely not so good. You try to initialise
the member '_out' before 'm_out', but in fact, they are initialised in
the order they are declared in the class, IOW 'm_out' is initialised
first, then '_out'. What happens in that case is that 'm_out' is getting
its value from an uninitialised pointer. What you need is to swap the
declaration statements for 'm_out' and '_out' in your class definition.
i can make a objects of this class like:
ofstream ofs("test");
myClass mclass(ofs);
or:
myClass mclass();
No, that would be a function declaration.
or:
myClass mclass("test");
and all will be good
No, it won't be good.
while i do not decide to make new member function
like this changeFile(stri ng &newfilename ), then i can write the next
code:

void changeFile(stri ng &newfilename )
{
_out = auto_ptr<ofstre am>(new ofstream(newfil ename));
// now _out is ok
m_out = *_out;
// this is not valid; the reference to object can't be changed
// in write function i will be need make checks and run *_out << text
// i don't like this variant
}

are you can help me?


No, I am can't help you. References, once initialised, cannot be
changed to refer to something else. You need to drop the 'm_out' and
always use (*_out) in your code.

V
Jul 23 '05 #2
Hi Victor,
What you thing about this modifications:

class myClass
{
public:
myClass(ostream &out=cout) : m_out(out) { }
myClass(string &filename) :
_out(new ofstream(filena me.c_str())),
m_out(*_out) { }
~myClass() { }

write(cons string &text) { m_out << text; }
void changeFile(stri ng &newfilename )
{
this->~myClass();
new (this) myClass(newfile name);
}

protected:
auto_ptr<ostrea m> _out;
ostream &m_out;
}

and again:

ofstream ofs("test");
myClass mclass(ofs);
or:
myClass mclass;
or:
myClass mclass("test");

Regards,
Vladimir

Jul 23 '05 #3
Hi Victor,
What you thing about this modifications:

class myClass
{
public:
myClass(ostream &out=cout) : m_out(out) { }
myClass(string &filename) :
_out(new ofstream(filena me.c_str())),
m_out(*_out) { }
~myClass() { }

write(cons string &text) { m_out << text; }
void changeFile(stri ng &newfilename )
{
this->~myClass();
new (this) myClass(newfile name);
}

protected:
auto_ptr<ostrea m> _out;
ostream &m_out;
}

and again:

ofstream ofs("test");
myClass mclass(ofs);
or:
myClass mclass;
or:
myClass mclass("test");

Regards,
Vladimir

Jul 23 '05 #4
vovata wrote:
What you thing about this modifications:

class myClass
{
public:
myClass(ostream &out=cout) : m_out(out) { }
myClass(string &filename) :
You should pass the string by a ref to const:

myClass(string const &filename) :
_out(new ofstream(filena me.c_str())),
m_out(*_out) { }
~myClass() { }

write(cons string &text) { m_out << text; }
'write' has no return value type. I recommend

ostream& write(const string &text) { return m_out << text; }
void changeFile(stri ng &newfilename )
Again, by a ref to _const_.
{
this->~myClass();
new (this) myClass(newfile name);
Well, legally this trick is allowed. And it's known to work.
Personally I don't like it.
}

protected:
auto_ptr<ostrea m> _out;
ostream &m_out;
The reordering of _out and m_out in the class definition is certainly
what you ought to do.
} ;

It looks OK, and is OK if it works for you. I just cringe every time
I see somebody uses that 'call the destructor and re-construct in place'
trick. Ah, don't mind me...

and again:

ofstream ofs("test");
myClass mclass(ofs);
or:
myClass mclass;
or:
myClass mclass("test");


It will be OK as soon as you change the 'string&' to 'string const&'.

V
Jul 23 '05 #5

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

Similar topics

5
1755
by: Andy | last post by:
Hello All, I'm currently getting into ASP and need some advice on any particular books or tutorials or websites that you can recommend so that I can get a good foundation on ASP as well as intermediate and advanced knowledge on the subject too. I would appreciate if you can point me to some books or websites or tutorials regarding this.
11
2120
by: Mark | last post by:
Hi, For the last 2 years I've been developing vehicle tracking/telemetric software for a company as a self employed individual. The project is quiet big, and is going to be there flagship product. Since I had no other work at the time, I agreed to a low rate when I first started which was £100 a day. The project started small, so it was only going to be for a few months, and I badly needed the money. The project soon balooned into...
2
1657
by: andyjgw | last post by:
Hi I'm a bit new to the designing of custom web page controls and using them in the properties designer window - need a little advice on a concept here. I have two properties in my control - one that is a server name, which when entered will populate a drop-down list for the second property. I know that to get a drop-down list I will either need my second
1
1563
by: Chris Lane | last post by:
Need Advice on prebuilt Exception Assemblies Please take a look at my post on the Titled: Need Advice on prebuilt Exception Assemblies posted on 04/21/04 Thank
3
3179
by: Sigmathaar | last post by:
Hi, I'm need some advice about lists and vectors. I'm doing a program who needs to have sequential access of a non ordered unit of objects whose size decreases almost each time the sequence is finished (at the begining I have about 2500 objects in the unit, after the first acces I have 2499, then 2435, then 1720 and so on). The problem is that sometimes after a whole sequence the number of objects in the unit remains unchanged. After...
4
2855
by: Web_PDE_Eric | last post by:
I don't know where to go, or what to buy, so plz re-direct me if I'm in the wrong place. I want to do high performance integration of partial differential eqns in n dimensions (n=0,1,2,3..etc) I want to do (fast) graphic output of the results. I used to use C. I want to upgrade my computer. Do I get dual core? Does C# support dual-core? Is C# as fast as the old C? Is there a new C? (or is C# the new version of C?) Is Visual Studio...
9
1543
by: laststubborn | last post by:
Dear All, We have a big concern in our Database system. We have 2000 transactions daily in our database. We need to replicate some how the database for our fail over setup. I tried transactional replication at midnight but our all systems locked and we had a lot of complaints from the customers and It was taking a lot of time to snapshot part and I had to abort it because of these reasons. I need an advice how I can create a replication...
7
2064
by: John Paul | last post by:
I'm thinking of building an e-commerce site in php. Anyone got any advice in building one? What is the best way to implement a payment system? Are any legal issues involved? Thanks,
51
2610
by: cool_ratikagupta | last post by:
hello friends i ha just started learning c can u all give me the tips to make myself strong in c lanuage . as i want to be the best in watever i do . so just a request from all of u here plz help me out in improving my c .plz guide me as a techer . to do my best in c lanuage . i m clear with the concepts i just want to improve in c . as programming is best when u use less memory space n less ilterations.
7
2170
by: SM | last post by:
Hello, I have a index.php template (2 columns). The right columns contains a bunch of links (interviews, poems, etc...) The left columns contains the actual article. So if I click on a link on the right menu, the article shows on the left column. The links have an url that look like this: http://www.myweb/library/?doc=194uf7s39
0
9705
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
10320
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...
1
10308
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10073
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...
0
9134
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7609
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
6846
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5513
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...
2
3806
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.