can this be achieved

M

muser

while( strcmp( str_ptr.Determinefunction().customercode, '\0') );

str_ptr is a union pointer, pointing to a specific structure member.
Determinefunction() is a function, what I would like to know is
whether

1. Determinefunction has to be a member function of the union to work
in the code above.

2. Would i have to use scope resolution
str_ptr::Determinefunction.customercode

Thank you in advance for your help.
 
V

Victor Bazarov

muser said:
while( strcmp( str_ptr.Determinefunction().customercode, '\0') );

str_ptr is a union pointer,


What's a "union pointer"? Do you mean a pointer to a union?
pointing to a specific structure member.

Why don't you just show us the declaration of 'str_ptr'?
Determinefunction() is a function, what I would like to know is
whether

1. Determinefunction has to be a member function of the union to work
in the code above.

Depends on what you mean by all those "pointer" things and what
operator[] returns for it.
2. Would i have to use scope resolution
str_ptr::Determinefunction.customercode


Use it for what?

Victor
 
J

Jeff

while( strcmp( str_ptr.Determinefunction().customercode, '\0') );


Wow, that's really broken.

You're comparing customercode (God only knows what type that is) with
a 0-valued integer. The standard version of strcmp expects pointers
to null-terminated arrays of characters. That '\0' will be converted
implicitly to a null pointer, and you're going to get a segmentation
fault (assuming you manage to compile and run this code).

Also, why are you using strcmp? Use something with an overloaded ==
operator, e.g. the std::string class.
str_ptr is a union pointer, pointing to a specific structure member.

Good Christ, man, why are you using unions? To use a union properly,
you generally need an accompanying variable to tell which element of
the union is currently in use. Such "type id" variables are generally
bad ideas, mostly because they make the code difficult to read.

Don't get me wrong; unions aren't as horrible as some folks believe,
but the valid uses are certainly few and far between.
Determinefunction() is a function

No kidding? Hey, here's a nifty idea: put all your functions in a
separate namespace, called "function". (I'm just kidding, don't
really do that.)
what I would like to know is whether

1. Determinefunction has to be a member function of the union to work
in the code above.

Even if it is, that code won't work. You need ptr->method(), not
ptr.method. In that case, yes, the method must be a method of the
object to the left of the dereferencing operator (->).
2. Would i have to use scope resolution
str_ptr::Determinefunction.customercode


No, the only things that should ever go to the left of the scope
resolution operator :):) are type names and namespaces.
Thank you in advance for your help.

Good luck to you. It looks like you've got a hard road ahead of you.

Please, for your own benefit (and the sanity of those who must read
your code), read _The_C++_Programming_Language_.

-Jeff
 
M

muser

Victor Bazarov said:
muser said:
while( strcmp( str_ptr.Determinefunction().customercode, '\0') );

str_ptr is a union pointer,


What's a "union pointer"? Do you mean a pointer to a union?
pointing to a specific structure member.

Why don't you just show us the declaration of 'str_ptr'?
Determinefunction() is a function, what I would like to know is
whether

1. Determinefunction has to be a member function of the union to work
in the code above.

Depends on what you mean by all those "pointer" things and what
operator[] returns for it.
2. Would i have to use scope resolution
str_ptr::Determinefunction.customercode


Use it for what?

Victor


I take it there is nothing called a union pointer i.e. union
Allrecords *str_ptr1.
Allrecords recs;
*str_ptr1 = recs;

Ignore the above post as I wouldn't try a member function with a union
now,

but i have done this.

struct crecord {
union Allrecords {
char customercode[5];
};
/*.....*/
};

stroupstrup rates this approach. My problem will be accessing
customercode. is it *str_ptr1->customercode; or something entirely
different from that?
 
M

muser

while( strcmp( str_ptr.Determinefunction().customercode, '\0') );


Wow, that's really broken.

You're comparing customercode (God only knows what type that is) with
a 0-valued integer. The standard version of strcmp expects pointers
to null-terminated arrays of characters. That '\0' will be converted
implicitly to a null pointer, and you're going to get a segmentation
fault (assuming you manage to compile and run this code).

Also, why are you using strcmp? Use something with an overloaded ==
operator, e.g. the std::string class.
str_ptr is a union pointer, pointing to a specific structure member.

Good Christ, man, why are you using unions? To use a union properly,
you generally need an accompanying variable to tell which element of
the union is currently in use. Such "type id" variables are generally
bad ideas, mostly because they make the code difficult to read.

Don't get me wrong; unions aren't as horrible as some folks believe,
but the valid uses are certainly few and far between.
Determinefunction() is a function

No kidding? Hey, here's a nifty idea: put all your functions in a
separate namespace, called "function". (I'm just kidding, don't
really do that.)
what I would like to know is whether

1. Determinefunction has to be a member function of the union to work
in the code above.

Even if it is, that code won't work. You need ptr->method(), not
ptr.method. In that case, yes, the method must be a method of the
object to the left of the dereferencing operator (->).
2. Would i have to use scope resolution
str_ptr::Determinefunction.customercode


No, the only things that should ever go to the left of the scope
resolution operator :):) are type names and namespaces.
Thank you in advance for your help.

Good luck to you. It looks like you've got a hard road ahead of you.

Please, for your own benefit (and the sanity of those who must read
your code), read _The_C++_Programming_Language_.

-Jeff



Jeff why did you bother to respond to my post at all if you didn't
know anything about unions in the first place. Why didn't you inform
me that the arrow operator doesn't work in conjunction with the union
pointer *str_ptr1. Or that I can't declare Union Allrecords *str_ptr1,
*str_ptr2, temp_str; Without the union being undefined, whatever that
should mean, my guess is that it requires some form of initialisation.

Why didn't you just say that if you do this.

struct crecord {
union {//anonymous union, ensures members are allocated at the same
memory address
char customercode;
};
char partnum;
/*....*/
}

void sort(crecord* accessunion)
{

while( strcmp(accessunion->customercode, '\0'))
etc etc

The integer value you found so bizarre, was actually a part of a
sorting algorithm provided for by my course. So I'm sure it makes
sense to someone, even if that person isn't you. In future please tone
down your ignorant bombastic nature and present the facts only.
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top