473,405 Members | 2,210 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,405 software developers and data experts.

Initialized object solutions


Two templates. The first is more convenient, but you can't use it with
intrinsics. The second is less convenient, but it can be used with *all*
types. Here's the templates:

template<class T>
struct ValueInitialized : public T
{
ValueInitialized() : T() {}
};
template<class T>
struct ValueInitialized_WorksWithIntrinsics
{
T object;

ValueInitialized_WorksWithIntrinsics() : object() {};
};

And here's some sample usage code:
template<class T>
void GiveMeAnyType()
{
ValueInitialized<T> t;
}
struct SamplePOD
{
char a;
void* p_cheese;
signed milk;
};

struct NonPODAggregate
{
std::string talk;
int r;
};

int main()
{
GiveMeAnyType<std::ostringstream>(); //Has a private copy constructor
GiveMeAnyType<SamplePOD>();

GiveMeAnyType<NonPODAggregate>();

//GiveMeAnyType<int>(); //Opps! Can't inherit from "int"

ValueInitialized_WorksWithIntrinsics<int> rans;

int& jack = rans.object;

ValueInitialized_WorksWithIntrinsics<int> const season;

int const & winter = season.object;

}

Thoughts?
-JKop
Jul 22 '05 #1
5 1065

"JKop" <NU**@NULL.NULL> wrote in message
news:03*******************@news.indigo.ie...

Two templates. The first is more convenient, but you can't use it with
intrinsics. The second is less convenient, but it can be used with *all*
types. Here's the templates:

template<class T>
struct ValueInitialized : public T
{
ValueInitialized() : T() {}
};
template<class T>
struct ValueInitialized_WorksWithIntrinsics
{
T object;

ValueInitialized_WorksWithIntrinsics() : object() {};
};

And here's some sample usage code:
template<class T>
void GiveMeAnyType()
{
ValueInitialized<T> t;
}
struct SamplePOD
{
char a;
void* p_cheese;
signed milk;
};

struct NonPODAggregate
{
std::string talk;
int r;
};

int main()
{
GiveMeAnyType<std::ostringstream>(); //Has a private copy constructor
GiveMeAnyType<SamplePOD>();

GiveMeAnyType<NonPODAggregate>();

//GiveMeAnyType<int>(); //Opps! Can't inherit from "int"

ValueInitialized_WorksWithIntrinsics<int> rans;

int& jack = rans.object;

ValueInitialized_WorksWithIntrinsics<int> const season;

int const & winter = season.object;

}

Thoughts?


I´m somehow missing the credit to the people who showed you this ;-)

Cheers
Chris
Jul 22 '05 #2
Chris Theis posted:

"JKop" <NU**@NULL.NULL> wrote in message
news:03*******************@news.indigo.ie...

Two templates. The first is more convenient, but you can't use it with
intrinsics. The second is less convenient, but it can be used with *all*
types. Here's the templates:

template<class T>
struct ValueInitialized : public T {
ValueInitialized() : T() {} };
template<class T>
struct ValueInitialized_WorksWithIntrinsics {
T object;

ValueInitialized_WorksWithIntrinsics() : object() {}; };

And here's some sample usage code:
template<class T>
void GiveMeAnyType()
{
ValueInitialized<T> t; }
struct SamplePOD
{
char a;
void* p_cheese;
signed milk;
};

struct NonPODAggregate {
std::string talk;
int r;
};

int main()
{
GiveMeAnyType<std::ostringstream>(); //Has a private copy constructor
GiveMeAnyType<SamplePOD>();

GiveMeAnyType<NonPODAggregate>();

//GiveMeAnyType<int>(); //Opps! Can't inherit from "int"

ValueInitialized_WorksWithIntrinsics<int> rans;

int& jack = rans.object;

ValueInitialized_WorksWithIntrinsics<int> const season;

int const & winter = season.object;

}

Thoughts?


I´m somehow missing the credit to the people who showed you this ;-)

Cheers
Chris

Well I did mention in the other thread! :-P

For clarity:

These ideas came from:
Rob Williscroft
Tobias Güntner
-JKop
Jul 22 '05 #3

"JKop" <NU**@NULL.NULL> wrote in message
news:03*******************@news.indigo.ie...

Thoughts?
-JKop


I'm still confused as to what problem this is all supposed to solve.
Specifically, why, if you need your objects initialized, do you not have
constructors for them to initialize them as you desire? And for things like
local integer variables (etc.), you can always initialize them at
declaration time, right? I just don't see where I'd ever actually *need* to
use one of those templates. Perhaps I'm missing some special case where
neither of the approaches I've mentioned works? (Or perhaps you just enjoy
solving problems, whether they ever come up or not? I know I did, before I
had to work all day. :-))

-Howard
Jul 22 '05 #4
Howard posted:

"JKop" <NU**@NULL.NULL> wrote in message
news:03*******************@news.indigo.ie...

Thoughts?
-JKop


I'm still confused as to what problem this is all supposed to solve.
Specifically, why, if you need your objects initialized, do you not
have constructors for them to initialize them as you desire? And for
things like local integer variables (etc.), you can always initialize
them at declaration time, right? I just don't see where I'd ever
actually *need* to use one of those templates. Perhaps I'm missing
some special case where neither of the approaches I've mentioned works?
(Or perhaps you just enjoy solving problems, whether they ever come up
or not? I know I did, before I had to work all day. :-))

-Howard

I work... just not a programming - I ain't got that piece of paper that
says I can programme!
-JKop
Jul 22 '05 #5
JKop wrote:
Two templates. The first is more convenient, but you can't use it with
intrinsics. The second is less convenient, but it can be used with *all*
types. Here's the templates:

Thoughts?

As I said recently in the other thread, the initial problem does not
make sense, and thus any solution to it doesn't make sense either.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #6

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

Similar topics

1
by: TPSreport | last post by:
I log on to the cookieless application, work within it for awhile, jump to a URL outside the application, then return to the application. As soon as a db call is required, I get the error below. ...
3
by: Marc W | last post by:
Hello, I am trying to write some code, I get a compiler error, but the help about it is very limited. (I am working with MSVC++ 6.0.) Could you give me a hint what is wrong here please? ...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
2
by: Javaman59 | last post by:
I want to add a ValueChanged event handler to a control. If I add the handler during form construction then the ValueChanged handler gets called when the control is initialized, and I don't want...
2
by: lornejones | last post by:
I have the following error using vs.net 2003 C#: The ConnectionString property has not been initialized. Description: An unhandled exception occurred during the execution of the current web...
4
by: marcosnogood | last post by:
Hello, I need to dynamically load an activex object because what object to load is based on certain conditions. Also I need to wait for the object to have initialized before moving on. What I...
4
by: Nishu | last post by:
Hi All, What is the C-standard expected result for referring the 'uninitialized element' of the partial initialized automatic array ? /*************************/ #include <stdio.h> int...
2
by: phyton2 | last post by:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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,...

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.