|
This program has be written for an optimized ternary 2D vector addition
operation outlined by Bjarne Stroustrup (C++ Prog Lang, 3rd Ed, 1997,
pages 675-676)
<pre>
Conventional binary operation:
Define: class Point as given below.
Declare: Point p1(1,1), p2(3,3), p;
With a usual global binary addition operator (shown below within comments),
The statment p = p1 + p2; creates and destroys temporary point object.
</pre>
To avoid having to create such a temporary, Dr Stroustrup suggested a
method which has been made use of in writing this program. This involves
defining a temporary class to hold reference to two operands involved.
During the process of assignment, an inlined function is invoked.
The operation is equivalent to invoking an inlined function
<pre>
p.addAndAssign(p1, p2);</pre>
As this operation is also inlined, the net effect is equivalent to
component-wise addition:
<pre>
p.x = p1.x + p2.x; p.y = p1.y + p2.y;</pre>
Though beautiful this idea is, this code does not compile with VC++ 6.0
<pre>
The error messages are
error C2143: syntax error : missing ',' before '&'
error C2059: syntax error : '&'
at the line where the second overloaded operator= is defined:
...
>> Point& operator=(const PointsToBeAdded& pointsToBeAdded) {
...
}
</pre>
It looks like VC++ allows asignment operator = overloading
in which only possible argument is of the type
const reference to an instance of the class in which it is defined.
This also means that object conversion can not be achieved using a
custom assigment operator like:
<pre>
class A { public: int x, y; }
class B { public: int x, y;
B& operator=(const &A a) { // VC++ 6.0 compiler doesn't accept
x = a.y; y = a.x;
}
}
</pre>
Is there any way around this problem. Or is VC++ non-standard conforming.
Have I missed out any header files?
Thanks for taking time to read this posting.
Ganesh
<pre>
// TernaryOpTester.cpp :
#include <iostream>
using namespace std;
class Point
{
public:
int x, y;
Point() : x(0), y(0) {};
Point(int a, int b) : x(a), y(b) {}
~Point() {}
Point(const Point& p) : x(p.x) , y(p.y) {}
// function assignment: Point p, p2(2,2);
// p(2,2); is equivalent to p = p2;
void operator() (int a, int b) { x = a; y = b; }
// you can write for ex: p(p2);
void operator() (const Point& p) { x = p.x; y = p.y; }
Point& operator=(const Point& p) { x = p.x; y = p.y; }
Point& operator=(const PointsToBeAdded& pointsToBeAdded) {
addAndAssign(pointsToBeAdded.p1, pointsToBeAdded.p2);
return *this;
}
void addAndAssign(const Point& p1, const Point& p2) {
x = p1.x + p2.x;
y = p1.y + p2.y;
}
};
ostream& operator<< (ostream& os, const Point& p) {
os << "(" << p.x << ", " << p.y << ")";
return os;
}
// An instance hold reference to operands of binary +
class PointsToBeAdded {
public:
const Point& p1;
const Point& p2;
PointsToBeAdded(const Point& pp1, const Point& pp2) : p1(pp1), p2(pp2) {}
operator Point();
};
// p1 + p2 returns an instance of the above class which is used by
// the assigment operator of Point class
inline PointsToBeAdded operator+(const Point& p1, const Point& p2) {
return PointsToBeAdded(p1, p2); // no addition yet.
}
/*inline Point operator+(const Point& p1, const Point& p2) {
return Point(p1.x + p2.x, p1.y + p2.y);
}*/
int main(int argc, char* argv[])
{
Point p1(1,1), p2(4,4), p;
cout << "p1 = " << p1 << ", p2 = " << p2 << ", p = " << p << endl;
p = p + p1;
cout << "p = p1 + p2 = " << p << endl;
return 0;
}
</pre>
Ganesh
|
|
|
|
|
Hello, I think the problem is that, in the line where Point& operator=(const PointsToBeAdded& pointsToBeAdded) is defined, PointsToBeAdded has not even been declared yet. Please try the following arrangement:
class PointsToBeAdded;
class Point
{
...
Point& operator=(const PointsToBeAdded& pointsToBeAdded);
};
class PointsToBeAdded
{
};
Point& Point::operator=(const PointsToBeAdded& pointsToBeAdded)
{
addAndAssign(pointsToBeAdded.p1, pointsToBeAdded.p2);
return *this;
} Hope this helps.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
Hello, THanks for the reply. I figured this out only now.
I did try what you suggested before.
But MS VC++ does not accpept such a construct like GNU CC does.
But this problem can be solved by subclassing a simple Point class:
class Coords2D {
public:
int x,y;
Coords2D() : x(0), y(0) {}
Coords2D(int a, int b) : x(a), y(b) {}
Coords2D(const Coords2D& c) : x(c.x), y(c.y) {}
Coords2D& operator=(const Coords2D& c) { x = c.x; y = c.y; }
};
// An instance hold reference to operands of binary +
class PointsToBeAdded {
public:
const Coords2D& p1;
const Coords2D& p2;
PointsToBeAdded(const Coords2D& pp1, const Coords2D& pp2) : p1(pp1), p2(pp2) {}
//operator Point();
};
class Point : public Coords2D
{
...
};
...
and so on....
Here is the error message
TernaryOpTester.cpp
H:\C++\Projects\Test\tmp\TernaryOpTester.cpp(79) : error C2027: use of undefined type 'PointsToBeAdded'
H:\C++\Projects\Test\tmp\TernaryOpTester.cpp(58) : see declaration of 'PointsToBeAdded'
H:\C++\Projects\Test\tmp\TernaryOpTester.cpp(79) : error C2228: left of '.p1' must have class/struct/union type
H:\C++\Projects\Test\tmp\TernaryOpTester.cpp(79) : error C2027: use of undefined type 'PointsToBeAdded'
H:\C++\Projects\Test\tmp\TernaryOpTester.cpp(58) : see declaration of 'PointsToBeAdded'
H:\C++\Projects\Test\tmp\TernaryOpTester.cpp(79) : error C2228: left of '.p2' must have class/struct/union type
Tool returned code: 2
Ganesh
|
|
|
|
|
I am trying to create an RE that recognizes C-comment syntax.
Example: "/\*[^*]*\*/" recognizes any C-comment that does not have the character '*' within the comment text.
Problem: The character '*' needs to be recognized within the comment.
Esentialy I need an RE that acts like this:
bool IsComment(char* pStr)
{
if( !pStr || !*pStr || strncmp(pStr,"\\*",2) )
return false;
pString += 2;
while( *pStr && strncmp(pStr,"*//",2) )
++pStr;
return !strncmp(pStr,"*//",2) ? true : false;
}
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
Do you even need anything special in the middle of the regex? I'd think that the endpoint strings of /* and */ would be enough. Something like:
/\*.*?\*/
using a non-greedy match on .* to get only one comment. Using the default greedy match would treat /*foo*/ bar /*baz*/ as one big comment.
--Mike--
Visual C++ MVP
LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ
Strange things are afoot at the U+004B U+20DD
|
|
|
|
|
Well, I am not using Regex++ or any thing like that. The one I am using (mine), only supports basic REs with character classes like \w and [:word:], thrown in for good measure. So it does not recognizes that expression. I've seen that expression in Regex++, but don't understand how it works.
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
I'm most familiar with the Perl regex syntax, so that's what I was using. The ? after a wildcard match means "match as few characters as possible without making the entire match fail". The difference is apparent with that sample from my earlier post:
/*foo*/ bar /*baz*/
/\*.*\*/ matches /* then foo*/ bar /*baz then */ (note the .* slurps up as much as possible, this is the default "greedy" behavior)
/\*.*?\*/ matches /* then foo then */ (the .*? only slurps up "foo" because it can stop there and still have the entire match succeed)
If you know what your input will be, and know that there will never be two comments on the same line, you should be able to go with /\*.*\*/
--Mike--
Visual C++ MVP
LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ
Strange things are afoot at the U+004B U+20DD
|
|
|
|
|
Thanks for the explanation.
I went to write simple color coded text editor a few years ago and ended up with a code editor for C/C++ and Pascal, that acts a lot like the VC++ editor. I am trying to make the backend user programmable via regular expressions, so the user can set it up for any language.
At the moment a programer can program it, within limits, using a built-in text state class. But this method is flawed and not user friendly enough for me.
Note: I have no way to know what the input will be.
Thanks again!
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
When I transfer a file, I immediately follow the transfer with a "MTDM" (change file time) command so the file reflects the time it was actually last modified on my machine.
The server always reports that the file wasn't found, even though I got a 226 (Transfer OK) response after the transfer completed. Do I have to flush a cache or some such thing?
------- sig starts
"I've heard some drivers saying, 'We're going too fast here...'. If you're not here to race, go the hell home - don't come here and grumble about going too fast. Why don't you tie a kerosene rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
hi
does anyone has an expirience in programming using parasolid kernel?
i need a light samples on subject, programming reference etc.
google is really poor on this....
all help will be highly appreciated.
4apai
There're no impossible tasks. There're tasks that required infinite period of execution time.
|
|
|
|
|
Hi to all,
I want to change the predefined text language in Message Boxes an Property Sheets.
I dont know how to do.
I would be pleased if anyone helps me.
Thanks
Ali
|
|
|
|
|
|
Suppose i use __penter compiler feature in my project in order to have function hooking.
I'd like to get the function/method name by the ebp registry value.
I know that i need to use SymGetSymFromAddr. However, i'm not familiar with this very much.
Can anyone help me get the function/method name by the ebp registry value?
Thanks,
Dudi
|
|
|
|
|
|
i have a visual studio project in c++ that makes a .exe i want to run it from the command line, i think i need to complile it from here as well, how do i do that,what is the command?
|
|
|
|
|
An .EXE file is an EXEcutable. Just run it; you don't need to compile it again.
Software Zen: delete this;
|
|
|
|
|
try following:
cd "..\MyProject"
"C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE\devenv" /nologo myproject.sln /rebuild "Release"
4apai
There're no impossible tasks. There're tasks that required infinite period of execution time.
|
|
|
|
|
Hi,
I have problem in using variable argument (...) in win32 DLL.
If you have a solution or any other suggestion or tutorial, please help me .
When I wanna to call the function that gets variable arguments in compiling the test application it has a linking error.
Regards.
|
|
|
|
|
the information u provide get me nothing to help with solving
some code?
4apai
There're no impossible tasks. There're tasks that required infinite period of execution time.
|
|
|
|
|
hi,
My problem was solved!
thank you
|
|
|
|
|
i use vc6.0 to access data table in Access of MS, but the data table that i will access is built by the other program. i want to get the new data that program put into the table. because the program often put data into the table, i want to know when it change the table, and i want to get the new data that is put into table after the table is changed. Can Access of MS inform me?
|
|
|
|
|
in ms sql server the solution is to implement triggers to the tables changes u want to be notified. i dont know about access. does it support this...
4apai
There're no impossible tasks. There're tasks that required infinite period of execution time.
|
|
|
|
|
Hi to all, I am programming a dialog based application. My problem is, if I close the program, I want to prompt a message box asking the user if they really want to exit the application, if the user choose no, the application continues, if the user choose yes, the application exits.
Thank you in advance,
quistiun
|
|
|
|
|
Try overriding OnClose() and either call CDialog::OnClose() or just return, based on the return value of your confirmation dialog box.
Steve
|
|
|
|
|
Tnx a lot... I'll try it..
|
|
|
|
|