473,320 Members | 1,930 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.

Using class inside itself

I have a class:

class Complex {
public:
double x, y;

Complex () {x = 0.0; y = 0.0;};

Complex operator * (Complex);

Complex operator / (Complex);

Complex conj (void);
private:
};

Complex Complex::operator / (Complex a) {

Complex b;

b = me * a;

/* by me, I mean this class, so that x == me.x and y == me.y */

}
So how I reference to this class whom inside I am?

So that I don't need to redefine multiplying again when using it within
the class.
Jul 23 '05 #1
3 2561
Tatu Portin wrote:
So how I reference to this class whom inside I am?


*this

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #2
Tatu Portin wrote:
I have a class:

class Complex {
public:
double x, y;

Complex () {x = 0.0; y = 0.0;};

Complex operator * (Complex);

Complex operator / (Complex);

Complex conj (void);
private:
};

Complex Complex::operator / (Complex a) {

Complex b;

b = me * a;

/* by me, I mean this class, so that x == me.x and y == me.y */

}
So how I reference to this class whom inside I am?


You mean "object", not "class". You can access it through an implicitly
defined pointer named 'this'. So your function becomes:

Complex Complex::operator / (Complex a) {

Complex b;

b = *this * a;
//...
}

Another way is to call the operator by its "full" name, like:

b = operator*(a);

But i'd recommend to not write operators like / as member operators. Rather
make them non-members.

Jul 23 '05 #3
On 2005-05-30, Tatu Portin <ax****@mbnet.fi> wrote:
I have a class:

class Complex {
public:
double x, y;
these should be private
Complex () {x = 0.0; y = 0.0;};
Complex() : x(0.0),y(0.0) {}
Complex operator * (Complex);
operator* should be a non-member.

Make operator*= a member. Pass arguments by const ref,
not by value

e.g. Complex& operator*= (const Complex& z) {
double xnew = x*z.x-y*z.y; y = x*z.y+y*z.x; x = xnew; return *this;
}
Complex Complex::operator / (Complex a) {

Complex b;

b = me * a;

/* by me, I mean this class, so that x == me.x and y == me.y */


No, you mean this object. Use the "this" keyword which references a pointer
to the calling object. So to multiply the calling object by a, write:
b = *this * a;

btw, the above code does not do division. You need to multiply by the inverse
of a which is the complex conjugate over the length squared

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #4

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

Similar topics

12
by: Gaurav Veda | last post by:
Hi ! I am a poor mortal who has become terrified of Python. It seems to have thrown all the OO concepts out of the window. Penniless, I ask a basic question : What is the difference between a...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
3
by: nicver | last post by:
I am fixing a client's Web site and for some reason an ASP class does not want to use the variables it retrieves when it initialiases. This is an excerpt of the class and it is enough to show...
10
by: George G. | last post by:
Hi there, I am busy writing a new asp.net application and I am reusing some of my existing asp functions and methods in a user control. I need access to session, request and response in some of...
20
by: Scott M. | last post by:
What are the advantages of defining a class as part of another class definition (nesting classes)?
19
by: hamil | last post by:
I have a form with one button, Button1, and a Textbox, Textbox1 I have a class, class1 as follows. Public Class Class1 Public DeForm As Object Sub doit() DeForm.Textbox1.text = "It works"...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
22
by: phil.pellouchoud | last post by:
I did some searching online and i couldn't find anything in reference to this. I am using MinGW, gcc 4.3 and am having the following compilation issue: class CFoo { public: ...
15
by: r0g | last post by:
Hi There, I know you can use eval to dynamically generate the name of a function you may want to call. Can it (or some equivalent method) also be used to do the same thing for the variables of a...
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: 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....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.