storing return value not using assignment nor memcpy?

G

gsyoon

hi, all.

I'm trying to make a "framework" to store the return value of a
function to a global memory.

My first attempt was

1)
void store_to_global( char * type_name ) {
if ( strcmp( type_name, "CLASS_A") )
previously_allocated_mem = func() ;
...
}

However, there are some classes that override their
assignment operator and making it non-public.
(using copy constructors don't help much as they can
be overridden and made private).

So, I tried to memcpy as

2)
void store_to_global( char * type_name ) {
if ( strcmp( type_name, "CLASS_A") )
memcpy ( previously_allocated_mem, (void*) (&(func())), sizeof
(CLASS_A) ) ;
...
}


2) worked fine for gcc. However, for Solaris CC,
it caused a compile error like
"Error: The "&" operator can only be applied to a variable or other
l-value."

How can I make a general framework to store the return value of
function
regardless of their operators being overridden and made non-public
and the compilers used?

Any comments are welcomed.
Thanks in advance.

Gwang Sik Yoon.
 
A

Alipha

gsyoon said:
hi, all.

I'm trying to make a "framework" to store the return value of a
function to a global memory.

My first attempt was

1)
void store_to_global( char * type_name ) {
if ( strcmp( type_name, "CLASS_A") )
previously_allocated_mem = func() ;
...
}

However, there are some classes that override their
assignment operator and making it non-public.
(using copy constructors don't help much as they can
be overridden and made private).

So, I tried to memcpy as

2)
void store_to_global( char * type_name ) {
if ( strcmp( type_name, "CLASS_A") )
memcpy ( previously_allocated_mem, (void*) (&(func())), sizeof
(CLASS_A) ) ;
...
}

memcpying classes is a huge no-no. you're making a copy without going
through "the proper channels" (invoking the copy constructor) and so
you're bypassing the constructor code that will perform a *proper*
copy. And so, the destructors will end up trying to free the same
memory, or fun stuff like that.
2) worked fine for gcc. However, for Solaris CC,
it caused a compile error like
"Error: The "&" operator can only be applied to a variable or other
l-value."

How can I make a general framework to store the return value of
function
regardless of their operators being overridden and made non-public
and the compilers used?

Any comments are welcomed.
Thanks in advance.

Gwang Sik Yoon.

personally, I don't see how such a general framework would be useful,
and why you'd need it to work on types that don't have an accessible
operator= (you can impose at least a couple minimal requirements, can't
you?). And to top it off, you stuff it into a global variable.

anyway, if the function is returning by value, then it would be a
requirement that the type has a copy constructor.

p = std::auto_ptr<T>(new T(func)); // perhaps or something

if the function returns a reference, then you'd be able to get a
pointer to the object and hold onto that perhaps? of course, hard to
say when the pointed-to object would get destroyed and your pointer
would become invalid.

So now you have two different scenarios you'd have to code for. perhaps
use template specialization to execute one set of code if the function
returns a value, and the other if the function returns a reference?
 
R

Ram

I'm trying to make a "framework" to store the return value of a
function to a global memory.

I fail to understand what such a framework would be useful for. You
anyway need to refer to the global memory to retrieve your value. Are
you trying to save some space on local variables? Having return value
in a global memory is not a good idea. You might be in trouble if such
a code is used in a multithreaded application.

-Ramashish
 
G

gsyoon

Thanks Alipha,

(This is my first reply to any posting with google's news interface and
I'm a little bit worried if it would appear easy to read. ;))
memcpying classes is a huge no-no. you're making a copy without going
through "the proper channels" (invoking the copy constructor) and so
you're bypassing the constructor code that will perform a *proper*
copy. And so, the destructors will end up trying to free the same
memory, or fun stuff like that.

I do know that copying an object has many possible hazards. However, my
top-priority mission is to store the returned object by the function
programmed by anonymous programmers so that I can take a look into as
many details about the object as possible. :(
personally, I don't see how such a general framework would be useful,
and why you'd need it to work on types that don't have an accessible
operator= (you can impose at least a couple minimal requirements, can't
you?). And to top it off, you stuff it into a global variable.

I have to handle that case when an perverted programmer writes a
function that returns a class that have no such operator. Though
perverted, I cannot deny him/her as my customer :(
If there's no other way, I guess, I have to follow your advice to
impose restrictions. But I have to try all that I can do, haven't I? :)

(global variable is not really what I use. It's introduced to make it
simple.)





anyway, if the function is returning by value, then it would be a
requirement that the type has a copy constructor.

p = std::auto_ptr<T>(new T(func)); // perhaps or something

if the function returns a reference, then you'd be able to get a
pointer to the object and hold onto that perhaps? of course, hard to
say when the pointed-to object would get destroyed and your pointer
would become invalid.

So now you have two different scenarios you'd have to code for. perhaps
use template specialization to execute one set of code if the function
returns a value, and the other if the function returns a reference?

Thanks for your advice. I really appreciate it.
But I hope there might be simpler way to do what I want to do.
 
G

gsyoon

The term global variable was introduced to make the description of the
problem simple.
but I guess it did more harm than good.
Actually, it's a dynamically allocated memory.

I'm saving the return value of a function for later inspection after
the scope of
the returned value.

Anyway, thanks for your advice Ramashish.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top