473,320 Members | 1,861 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,320 software developers and data experts.

comparing two objects of the same class

33
in the test core i was given 2 objects
eg:
base a;
base b (parameter);

both have the same member functions so how to differentiate between these two objects.given that a test core of

a.somefunction(b);

in this case somefunction(cons &x) where we can simply say x is b. but how to indicate a?

i tried this method but it didnt work.
the error says cannot convert 'this' pointer from 'const base' to 'base &'

Expand|Select|Wrap|Line Numbers
  1.  
  2. if (this->somefunct() == x.somefunction() ){
  3.    do something;
  4. }
  5.  
  6.  
Oct 6 '09 #1
6 5382
weaknessforcats
9,208 Expert Mod 8TB
First, your example does not consistently use somefunction(). It uses somefunct() in one case.

Second, base::somefunction takes an argument. One object is known only by the this pointer. That being the case *this would be the object itself.

Third, somefunction uses a const base& as an argument. When you supply an object, the compiler is worried that somefunction will attempt to alter the const reference. You need to reassure the compiler that somefunction won't try to do that. Here you need to read up on constant member functions.
Oct 6 '09 #2
hanna88
33
thanks for the reply.sorry for the confusion

there's typo in my code above.
what im trying to do here is in "somefunc" method : do the code below

Expand|Select|Wrap|Line Numbers
  1.  
  2. if (this->otherfunc == x.otherfunc){
  3. do something};
  4.  
  5.  
i've been trying to google it but it didnt really help.should i be looking on object for constructor or const member func in order to solve the problem? i'm stuck.
:[
Oct 6 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
if (this->otherfunc == x.otherfunc){
do something};
If otherfunc is a member function, you are not calling the functions since the () argument list is missing. Using just the function name is referring to the address of the function except fopr member functions. Which these are.
Oct 6 '09 #4
hanna88
33
i get what you mean there.thanks for pointing out my mistake.

another thing that bother me is how to relate two objects where both have the same member function.

Second, base::somefunction takes an argument. One object is known only by the this pointer. That being the case *this would be the object itself.
does this mean this only refer to a? and x = b when i did the test core of
ans=a.somefunc(b) for this funfction somefunc(const base &x)
Oct 6 '09 #5
weaknessforcats
9,208 Expert Mod 8TB
The thing with member functions is that accrding to the rules for calling functions, ALL of the arguments have to be present when the call is made. But with member functions, you don't have a explicit argument for the object to use with the function:

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.     public:
  4.       int Afunction();
  5. };
  6.  
  7. int main()
  8. {
  9.  
  10.      A obj;
  11.      obj.Afunction();
  12. }
So how does the compiler know which A object to use? The answer is that the address of the correct object is secretly supplied by the compiler as a hidden first argument. The first argument, therefore, is always the address of the object. Since the address varies by object, C++ uses this to represent the address of the object.

A::Afunction(), then, really has one argument: this.

That's why you can only use this inside a member function. You will get an error if you try to use anywhere else.
Oct 6 '09 #6
hanna88
33
now i understand.thanks for your help :)
Oct 6 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Donnal Walter | last post by:
I was very surprised to discover that >>> import datetime >>> x = datetime.date(2004, 9, 14) >>> y = datetime.datetime(2004, 9, 14, 6, 43, 15) >>> print x == y True How can these two...
1
by: scott | last post by:
hi all, in c++ i could overload an == opperator and then using a template class i could compare the 2 classes using the overloaded opperators and you did not have to tell the template what class...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
3
by: Mark Denardo | last post by:
Hi I have an app that has a number of textboxes (richtextboxes to be exact) that get created dynamically and I add a few at a time to panel controls (so I can flip a group in and out when I want). ...
5
by: ma740988 | last post by:
There's a need for me to move around at specified offsets within memory. As as a result - long story short - unsigned char* is the type of choice. At issue: Consider the case ( test code ) where...
19
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as...
10
by: netnet | last post by:
I have a collection of objects that I store in the session. Then I look in that collection to see if the collection.contains an object. it answers false. If I dont store that collection in the...
20
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
1
by: Erick | last post by:
I have a generic comparer class that i use to help me sort collections of any object in any order. It worked with Option strict off but now i need it to work with option strict on. Here is...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.