|
Dear all,
I cannot quite understand the logic behind the notion of operator overloading.
I understand that creating a class, is like creating a new type, therefore we can overload some operators to work with that new type. For example:
Ratio& Ratio::operator=(const Ratio& r)
{
num = r.num;
den = r.den;
return *this;
} This example is absolutely comprehensible but I cannot understand how I have to think, visualize the things in order to implement operator overloading in the *, *= operator, or in the == for instance.
Consider the Ratio class as the following:
class Ratio
{
friend Ratio& operator*(const Ratio& x, const Ratio y);
public:
Ratio(int =0, int =1);
Ratio& operator=(const Ratio&);
Ratio& operator*=(const Ratio&);
private:
int num, den;
};
Here is the understanding problem:
Ratio operator*(const Ratio& x, const Ratio& y)
{
Ratio z(x.num*y.num, x.den*y.den);
return z;
}
Is this the only way to overload that operator in this example? How one has to think in order to writew the above code? What are the steps involved in order to write the above two lines of code? And secondly in the first example (operator=) the return type is a reference to the Ratio but in the second is not a reference. Why is not a reference. When should we use a reference and when not?
Please bare in mind that I understand the use of friend functions.
Thanks in advance for the help,
grscot
|
|
|
|
|
Compilation of file1.cpp and file2.cpp - success.
Linking causes error LNK2001.
What are possible reesons?
//file1.h
void dosmth();
//file1.cpp
#include "file1.h"
void dosmth()
{
//dosmth
}
//file2.cpp
include "file1.h"
void main()
{
dosmth();
}
XPIS
|
|
|
|
|
Hi,
I couldnt find anything wrong in your code except missing of # in the #include statement @ file2.cpp. It is working 4 me. @ what env. r u working @
Sujan
|
|
|
|
|
I have about 50 int variables in my form to initialize. The variables are different only in the last 2 numbers. How can I concat the strings to hold int data types so that I wouldn't have to write out all 50 variables. I'm able to do this in ASP but not sure how to do it in C++.
CString str1 = "m_var";
CString str2;
for (int i=1; i <= 50; i++)
{
char num[10];
itoa(i,num,10);
str2 = str1 + num;
//need str2 to hold an int value
}
|
|
|
|
|
CString str;
str.Format("%i", int_nr);
//str will now be equal to int_nr even if str had another value
good luck.
"If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix
|
|
|
|
|
If I understand your question correctly, I can only offer this:
struct
{
int m_var1;
int m_var2;
int m_var3;
int m_var4;
int m_var5;
int m_var6;
int m_var7;
int m_var8;
int m_var9;
int m_var10;
} m_vars;
int *pVar = &m_vars.m_var1;
for (int x = 0; x < sizeof(m_vars) / sizeof(int); x++)
{
*pVar = 1;
pVar++;
}
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
|
I'd like to customize the CFileDialog to add some extra controls to the bottom of it. When the dialog first opens, I'd only like one of the extra controls to be displayed. When the user selects this control (a check box), then I'd like the dialog to expand to show the rest of the controls.
I am using the explorer style CFileDialog with a custom template. If I place all of the extra controls on the custom template, the size of the CFileDialog is extended to include all of these controls (even if they are not visible). So I get the CFileDialog w/ a bunch of extra gray space at the bottom of it.
I thought that I could make the invisible controls very small in the custom template to minimize this extra gray space. Then when they need to be seen, I'd resize the CFileDialog and the controls and set the controls visible. The problem is that whenever I change the size of the CFileDialog by calling SetWindowPos(), the original CFileDialog controls resize to fill up the extra space, leaving no extra room for my new controls.
Does anyone know how to customize the CFileDialog to allow for collapse/expand at the bottom of the dialog? If so, I could really use some suggestions. Thanks!
|
|
|
|
|
Hi,
I needed help with resizing a CStatic box to fit exactly the size of the text inside. The thing is once I increase the font size, I cant get the exact dimensions from GetTextExtent(). I've tried to multiply it with a ration, but no luck. It doesnt fit on it. Is there any function out there that can do this for me or any help on what would be a good way to do it. Help Needed ASAP.
A
|
|
|
|
|
GetTextExtent has always worked for me. Show your code and maybe we can help.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04
Within you lies the power for good - Use it!
|
|
|
|
|
I was just testing to see what font is currently stored in CDC object and it turns out that it is totally different from the font of the window. Why is that? and how do I change the font of CDC object as there is no setFont function there.
the code is below:
//set the font of the window text
CWnd* pCtrlWnd = GetDlgItem(id);
CFont * oldFont = pCtrlWnd->GetFont();
LOGFONT* pLogFont = new LOGFONT;
oldFont->GetLogFont(pLogFont);
CString fontName = pLogFont->lfFaceName;
CFont * font = new CFont;
font->CreateFont(newFontSize,0,0,0,700,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, fontName);
//get the draw context of window
pCtrlWnd->SetFont(font);
//resize the window
CDC* pDC = pCtrlWnd->GetDC();
CString text;
pCtrlWnd->GetWindowText(text);
//get the textExtent
CSize stringSize = pDC->GetTextExtent(text);
pCtrlWnd->SetWindowPos(NULL, 0, 0, stringSize.cx * 1.4 , stringSize.cy * 1.45, SWP_NOMOVE|SWP_NOZORDER|SWP_NOREPOSITION);
}
A
|
|
|
|
|
Ali Niaz wrote:
how do I change the font of CDC object
SelectObject() will do that for you.
I see several problems with the code you supplied. The most obvious being the severe potential for memory leaks. See http://www.codeproject.com/gdi/drawtechniques.asp[^]. You might also want to make your font a member variable of your static control's parent window.
CDC* pDC = pCtrlWnd->GetDC();
CString text;
pCtrlWnd->GetWindowText(text);
CFont *pOld = pDC->SelectObject(font);
CSize stringsize = pDC->GetTextExtent(text);
pDC->SelectObject(pOld);
...
See also:
http://www.codeproject.com/cpp/nogetdlgitem.asp[^]
http://www.codeproject.com/gdi/csavedc.asp[^]
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04
Within you lies the power for good - Use it!
|
|
|
|
|
sorry its ERROR_ACCESS_DENIED
|
|
|
|
|
Hai,
I'm opening a serial port COM1 as follows..
static LPTSTR portname = "\\\\.\\COM1\0";
HANDLE hComPort = CreateFile( portname, GENERIC_READ |GENERIC_WRITE, //bidirectional
0, NULL, //no security OPEN_EXISTING, 0, NULL );
if (hComPort == INVALID_HANDLE_VALUE)
{
sprintf(errstr,"Could not open file (error %d)\n", GetLastError());
MsgBox(errstr);
}
But it is giving me ERROR_INVALID_HANDLE..
What did I miss...pls help..
thanks a lot.
|
|
|
|
|
Is COM1 in use by any other processes?
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
I don't think so...I have a controller connected to the com1..i'm trying to send data to the controller..
but how can i find whether any other process is using com1??
thanks a lot.
|
|
|
|
|
Try Portmon from sysinternals, freeware
"An expert is a person who has made all the mistakes that can be made in a very narrow field." - Neils Bohr
|
|
|
|
|
changing icon of an exe file by peplacing icon resource inside the exe file.
It is easy if using UpdateResource().
but my users use all OS including win95 and win98.
the program using UpdateResource() will not work on win95/98 because the function is not supportted on 95/98.
do you have idea to implement the task?
thx
includeh10
|
|
|
|
|
Check Erik Kallen's article Updating Resources on Win9x[^], which provides a replacement for UpdateResource and familiy in Win9x systems. I've used this library in the past and it works like a charm.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
thx
I will try it.
includeh10
|
|
|
|
|
I have a database program that I am working on that uses CString for a description of a product, is there anyway to get more than the 256 charecters or do I have to use a diffrent type of string?
|
|
|
|
|
CString has no size limit, other than available memory. Something else is creating the 256-char limit.
--
I'm Michael Dunn and I approve this post.
Vote Trogdor in oh-four!
|
|
|
|
|
Many Databases limit String fields to 256 characters.
They do offer "MEMO" fields with 64K/unlimited length, but typically they cannot be searched, or indexed.
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist Fold With Us! || Agile Programming | doxygen
|
|
|
|
|
I've just started to program Windows Forms apps in VC++ .NET.
I have prior experience in developing WinForms apps in C# .NET, and the confusion that leads to my question is rooted in the jump from C# to C++.
In C#, when I add a button to the form and create an event handler function this function is visible in my code file.
However, in VC++, the designer is the ".h" file and the InitializeComponent function is also in the ".h" file.
The event handler function is also added in the ".h" file.
My question is this:
What code is added in the project's ".cpp" file? By that, I mean to ask, what part of my program should be coded in the .cpp file?
How do I access the components, say btn1 , btn2 etc from the .cpp file.
|
|
|
|
|
Hi Friends,
How to get the items from the listcontrol on scrolling vertically.
I have dialog with listcontrol. I have created seperate class for listcontrol to catch veticall scrollbar. Once uset start scrolling vertically i have to fetch the row from the Listcontrol.
Please suggest me how to do this.
Your help is appreciated.
Please response to purushotham.chandrashekar@orange.co.uk.
Thanks in advance
|
|
|
|