Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-05-2003, 02:34 PM   #1
Registered: Jan 2003
Posts: 167

Rep: Reputation: 30
c++ friend help

I am writing a class to deal with complex numbers.

First of all, is it possible to overload "**" as an operator? I played around with it a lot, but it doesn't look like it's gonna let me do it.

If that doesn't work, I would like to have a function pow(Complex,int) that will raise 'Complex' to the power 'int'. I have it working o.k., but to use it in a main program, I have do use do something like:
Complex access, a(2,4);
Complex b = access.pow(a,2);
I am thinking that you can get around this with friend functions, but I can't seem to get the declerations right. Here's what I have:
class Complex
        friend Complex pow(Complex, int);
Complex Complex::pow(Complex number,int power)
{ ..... }
Is there a way to do this so that I can use the pow(Complex,int) function without calling it as a member function?


crap, nevermind....
Complex pow(Complex number,int power)

Last edited by PTBmilo; 04-05-2003 at 02:37 PM.
Old 04-05-2003, 06:03 PM   #2
LQ Guru
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
I'm not sure what you mean... there is no ** operator in C++. BASIC has an operator like that for exponentiation, but C++ only has the pow() function and ones like it. So to answer your question, there is no way to create a new operator - you can only overload existing ones. I suppose you could overload another one you don't need (like say, bitwise XOR: ^ ) to mimic exponentiation.
Old 04-06-2003, 03:20 AM   #3
Registered: Jan 2003
Posts: 167

Original Poster
Rep: Reputation: 30
hey, that's a good idea. Yeah, i know that there isn't a ** operator, but I was hoping that there would be a way to create one. thanks for the clarification.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
My friend has a problem. Draconian Linux - General 11 09-04-2004 04:17 PM
Which Type Of Friend Is Your ... rinard General 4 03-22-2004 05:36 AM
Friend and his Dad asterix_hax0r Linux - Newbie 2 03-01-2004 11:45 PM
Which Type Of Friend Is Your ... rinard General 4 01-11-2004 05:59 AM
My idiot friend again BajaNick Linux - General 9 07-29-2003 04:54 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:32 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration