473,473 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Constructors, references, DotNet Strings

RLF
Am upgrading a C++ console app to add a DotNet Front End. Have run into a
couple of issues.

The first one is basically a c++ question. I have a class which maintains a
reference to an istringstream that is passed to it in the constructor.
Since I won't have an istream to give it from the front end, I'd like to add
another ctor that accepts a string and generates the istringstream for me so
that the reference initializer is satisfied.
Haven't found an acceptible way to do that. Could use some help.

The second question involves the string itself. I presume the ctor will most
likely need to deal with a System::String coming from the front end. I am
not familiar with the conversions available to turn that into an acceptable
type that can be handled by the istringstream constructor.

Any advice would be appreciated.
Nov 17 '05 #1
1 1893
RLF wrote:
Am upgrading a C++ console app to add a DotNet Front End. Have run into a
couple of issues.

The first one is basically a c++ question. I have a class which maintains a
reference to an istringstream that is passed to it in the constructor.
Since I won't have an istream to give it from the front end, I'd like to add
another ctor that accepts a string and generates the istringstream for me so
that the reference initializer is satisfied.
Haven't found an acceptible way to do that. Could use some help.
You have a lifetime issue here. The existing ctor for your class X stores a
reference to an istringstream object which must outlive the X object, and
you want to add a ctor which creates the istringstream object. One way would
be to add a smart pointer to your class, e.g.

class X
{
public:

X(istringstream& str)
: m_str(str)
{
}

X(const std::string& s)
: m_pStream(new istringstream(s)),
m_str(*m_pStream)
{
}

// Everyone uses m_str

private:

smart_ptr<istringstream> m_pStream;
istringstream& m_str;
};
The second question involves the string itself. I presume the ctor will most
likely need to deal with a System::String coming from the front end. I am
not familiar with the conversions available to turn that into an acceptable
type that can be handled by the istringstream constructor.


Below is a short example which shows how to pass a System::String to the C
function puts. Once you have a pointer to an unmanaged string, you can
convert it to a std::string or whatever unmanaged type you like. See also
PtrToStringChars in the file <vcclr.h>.

#using <mscorlib.dll>
using namespace System;

#include <stdio.h>

void do_puts(String* s)
{
using namespace System::Runtime::InteropServices;

IntPtr p = Marshal::StringToHGlobalAnsi(s);
// Call unmanaged function.
puts(static_cast<const char*>(p.ToPointer()));
Marshal::FreeHGlobal(p);
}

int main()
{
String* s = S"doug";
// puts(s); // Fails - cannot convert from String* to char*
do_puts(s);
return 0;
}

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #2

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

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
2
by: Neil Zanella | last post by:
Hello, AFAIK the only way to initialize a reference variable defined inside a class is to initialize it in an initializer list. However, when there are multiple constructors, this means that the...
17
by: johny smith | last post by:
I never really know if I should use references or pointers for constructors. Can anyone give me some guidance on when to use what? My back ground is C, so I tend to use pointer notation, but...
6
by: Stephen Martinelli | last post by:
thanks for the help...just one more question.... can a class have more then two parameterized constructors?..i would like to be able to instanciate the class with a different number of...
14
by: Paul Bromley | last post by:
Forgive my ignorance on this one as I am trying to use a Singleton class. I need to use this to have one instance of my Class running and I think I understand how to do this. My question however is...
14
by: el_sid | last post by:
Our developers have experienced a problem with updating Web References in Visual Studio.NET 2003. Normally, when a web service class (.asmx) is created, updating the Web Reference will...
6
by: cj | last post by:
I'm tryin to set up a sqlcommand in VB.NET that would issue the command: insert into server1.database.owner.table select * from server2.database.owner.table Since this sqlcommand has it's...
7
by: fakeprogress | last post by:
For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that...
4
by: Siegfried Heintze | last post by:
Can one declare static constructors in VB like one can in C#? In C#, static constructors execute once before any instances have been created. Thanks, Siegfried
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
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...
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,...
1
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...
0
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...
0
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.