|
Why are you returning a 'Point' anyway?
If it's found, they've already supplied the point in the first place, so the return result doesn't give them anything new except another point that matches the one they supplied.
Maybe a 'bool HasPoint( Point point )' method may suit your needs, or you could return an integer index into the array (and -1 if it doesn't exist)?
If you really do want to return a point for some reason then how about:
<br />
class Box<br />
{<br />
private:<br />
Point points[40];<br />
<br />
public:<br />
bool FindPoint( Point search, Point &result );<br />
};<br />
n!
|
|
|
|
|
Thanks for the reply and reminding me my mistake.
Now consider the situation.
what if findPoint function now has this prototype
Point Box::findPoint(int x)
{
Point pTemp;
for(int i=0; i<40; ++i)
{
pTemp = points[i];
if(pTemp.getX() == x)
return pTemp;
}
return; // what to return here.
}
The purpose of the findPoint function is to find a Point which has its x data member equal to x provided. So, what to return
if the if condition not satisfied.
|
|
|
|
|
Ahh ok, then I'd pass in a reference to be filled in and return a bool.
<br />
bool Box::FindPoint( int x, Point &result ) const<br />
{<br />
for ( int i = 0; i < 40; ++i )<br />
{<br />
if ( points[i].GetX() == x )<br />
{<br />
result = points[i];<br />
return true;<br />
}<br />
}<br />
return false;<br />
}<br />
n!
|
|
|
|
|
Can we not use Java style here like this
this code fragment is of JAVA language
Point findPoint(int x);
so, in this case we can return null (cause null is a keyword in java) and also user defined types are reference types in java. My question now is
isnt there any alternative in C++ to this JAVA style other than provided by you???
|
|
|
|
|
All objects in Java are reference types, you're not returning by reference you're returning by value here. C++'s class model is different to Java, the closest built in primitive to a Java reference (which may be null, as you said) is a pointer (C++ references may not legally be null), which you said you'd like to avoid.
The alternatives I can think of at the moment are:
Throw an exception, of course, but that's a bit heavy handed for a failed search
Return an integer index (with -1 meaning not found) and provide an index operator.
Write a RefResult templated wrapper and return one of those, but that doesn't give you anything more than you'd get returning a pointer (except more code).
My previous suggestion (returning bool).
I can only say that C++ isn't Java and if you prefer the Java style then use Java?
n!
|
|
|
|
|
Thank you very much "n!"
I have decided to use your earliest suggestion (return bool). Your replies were really very much informative to me. I was really confusing Java and C++ memory management. This confusion is lowered very much by your replies.
Thank you.
|
|
|
|
|
You can do several things. First I would change it to:
bool findPoint( Point& ptFound, Point p );
Another option is to keep your original code and set the returned Point to a value that would never occur in practice. eg. -1,-1. Then you could write:
IsPointValid( ptFound = findPoint( point ) )
You can and should also make the function const. You can also return a const ptr, which doesn't break your OOP rule. Solution 3. Use const wherever possible.
There you - pick 1.
Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
|
|
|
|
|
Hi , hwo can i get a drive size,
Please reply,
Thanks
|
|
|
|
|
|
Have I lost my mind or is there no profiler in the Visual Studio .NET IDE?
If it is there, where is it? If not, does anyone know of a good one?
Thanks!
Marcus Spitzmiller
"Why must life be so hard? Why must I fail at every attempt at masonry?" - Homer
|
|
|
|
|
I think you have knocked at a wrong door.
You might find your solution in VS.Net IDE section.
|
|
|
|
|
duh - good point...Thanks
Marcus Spitzmiller
"Why must life be so hard? Why must I fail at every attempt at masonry?" - Homer
|
|
|
|
|
I've a CEdit class. When you press 'a' from the keyboard, it writes 'a' in the control which is obvious.
The problem is that I want to change this behaviour. when I press 'a' from the keyboard, some other character e.g. 'b' should be written. I have a 2D array that has mappings of all the keyboard keys. Actually I am dealing with Unicode (a multilingual application).
Suppose I want to replace 'a' character with unicode 23456, which message should I handle.
I have tried WM_KEYDOWN and WM_CHAR and changed the virtual key code. but it did not worked.
In short, please tell me any way in which I can display 'b' in the edit control when 'a' is pressed.
Saqib.
|
|
|
|
|
Try to override PreTranslateMessage
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Thanks alot for your help Greg. I got my problem solved. PreTranslateMessage function handed the whole MSG structure to me. I traped the WM_CHAR message and changed the wParam to 'b' when it was actually 'a'.
if (pMsg->wParam == 'a')
pMsg->wParam = 'b';
Codeproject is really amazing site. Thankyou once again.
Saqib.
|
|
|
|
|
You're welcome!
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
I created a CStringArray to hold some text. Whenever I serialize the array, i noticed there are some garbage in the text file. How do I fix this. I want to write carriage returns and newlines. I'm using a CFile object.
Thanks
|
|
|
|
|
Whenever you want text to appear on new line in file, use "\r\n" in a char type array or CString.
e.g. CString m_strLines;
m_strLines = "Line1.\r\nLine2\r\n";
I am not clear about the garbage that appeared in the file. Does the garbage characters appear at the end of your data or your data is not saved at all?
|
|
|
|
|
Hi, I have no problems with the carriage and line feeds. I have garbage character in from of all the text strings. ie
Text1
Text2
|
|
|
|
|
This is the class info from Serialize. The Serialize method stores the object info in a binary format, not a text format.
If you want to "serialize" your stringarray as a text file, you need to do this manually.
onwards and upwards...
|
|
|
|
|
|
One solution is template function.
template <<typename t="">>
void mydesiredfunction(myclass<t> p);
Kuphryn
|
|
|
|
|
Hello. I am a student and I am trying to write a library program for my school.
I have a big problem .... I don't know anything about doing a search engine for it! Can someone give me a website address which can help me or can someone post a code here or just give me the address of a tutorial (I searched for one but couldnt find).
Thank you!
|
|
|
|
|
Well, if you are using ODBC to connect to a database, then SQL is what you'll use to communicate with the database. As far as doing pattern matching in SQL, take a look at the LIKE clause.
Example, if I wanted all records with a last name that started with A , I would write a SQL statement like such...
SELECT * FROM Users WHERE LastName LIKE 'A%'; For online info, here's a link...
http://www.codeproject.com/database/simple_odbc.asp[^]
Jeremy Falcon
|
|
|
|
|