|
Like a lot of c++ newbies, I spent a little time struggling with pointers, the address-of business, and references. I think I got a handle on it now, so I wrote this addition operator for a matrix class...
// Matrix addition (overloads the plus operator)
// Notice that the matrix is passed as a reference.
// This will be less taxing if the matrices are really big.
// However, this leaves the matrix referred to here as toAdd
// vulnerable to changes, so we add a const to the parameter.
// Returns a reference as well
matrix& operator + (const matrix &toAdd)
{
// Create a zero matrix of the same size to output.
// Allocate the memory from the heap so it won't get
// deleted after this method is called (use "new")
matrix* output = new matrix(toAdd.get_rows(), toAdd.get_cols(), 0);
matrix& outref = *output; // Create a reference to the output matrix
// Don't forget to dereference output with *
// A temporary variable to hold the sum of each element
double val;
// Go through every element and sum
for(int i = 0; i < nrows; i++)
{
for(int j = 0; j < ncols; j++)
{
// Don't forget... the indices are always one less
// than the row and column numbers
val = elements[i][j] + toAdd.get_el(i+1,j+1);
output->set_el(i+1, j+1, val); // output is a pointer so use ->
}
}
// Just return a reference, rather than a giant matrix...
return outref;
}
Can someone with more experience than me please take a look at that and make sure there's nothing funny going on? I get the right result, but I want to make sure it's actually doing everything with references. The idea is that if I need to deal with very large matrices, I only want to pass references in, and pass a reference out.
Also, I was curious why the compiler threw an error if I did
matrix output = new matrix
rather than
matrix* output
I think I'm on the right track here. If I can get confirmation that everything's ok, I should be on my way.
Thanks everyone
-Mark
|
|
|
|
|
MALDATA wrote: Also, I was curious why the compiler threw an error if I did
matrix output = new matrix
Because new only creates pointers. matrix output; is fine, as is matrix output(/*constructor parameters*/);
Operator plus has two objects to work with, why don't you work with 'this', rather than create a new one ? At least, that's how I thought it worked.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I just did that to preserve the matrices being summed. If I use "this" will that change one of the matrices involved in the sum? Seems to me that it would, but I could be wrong. Is there really a benefit to doing it that way? I figure if I'm passing by reference anyway, the only thing that'll help is by saving the memory.
Thanks for the suggestions,
-Mark
|
|
|
|
|
MALDATA wrote: If I use "this" will that change one of the matrices involved in the sum?
No, the regular aritmetic operators should not change the operands.
It's a convention that operator overloads where the first operand is of type T , are made members of class T . You don't have to write the operators that way, but making them members can make the code easier to maintain since it keeps the matrix-related code closer together.
--Mike--
Visual C++ MVP
LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
|
|
|
|
|
Hi there,
I wanna read the boot sector of a floppy without using DeviceIoControl() function.So i just made a structure shown below;
struct boot_sector
{
char jump[3];
char name[8];
unsigned char secpercluster;
short int bytepersec;
short int reserved;
unsigned char fatcopy;
short int maxdirentry;
short int totalsec;
unsigned char mediadescriptor;
short int secperfat;
short int secoertrack;
short int noofsides;
long int hiddensectors;
long int hugesectors;
unsigned char biosdriveno;
unsigned char reservedsec;
unsigned char signature;
long int volumeid;
unsigned char volumelabel[11];
char filesystem[8];
}bt;
Now the size of the structure should be 62 bytes but vc++ compiler shows it to be 68 bytes .But i hav cross checked it using borland c++ compiler.In that also the sixe is 62 bytes. Can anybody help me out of this ,specially u nish. Suggest me something plzzzzz..
|
|
|
|
|
I'd say it has something to do with in memory padding. Why is it a problem ? Have you tried adding to parts of the struct using byte offsets to see where it's going wrong, if anywhere apart from the end ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I would agree with Mr. Graus.
Try
#pragma pack(1)
and it'll give you 62 in the Microsoft compiler
|
|
|
|
|
|
|
I am trying to develop MFC application with a CDialogBar control always active. I have added a CDialogBar object to the frame window. In the CDialog bar, I have few buttons and an image control. All works great, I can click on the button and some action takes place.
Now, I want to have an access to the button objects even if Modal dialog is showing or AfxMessageBox is showing. When a modal dialog box is called with DoModal(), internally, it gets the parent window and disables the window, which prevents the parent window from getting a message from the message pump. Since the CDialogBar is containd in the CFrameWnd object, it too gets disabled. But what I want is my CDialogBar to be enabled all the time, so it can recieved user actions, such as clicking on the button.
I tried to override the WM_ENABLE message of the frame window as well as the CDialogBar derived object, when WM_ENABLE message is posted, I ignore it if the parameter is FALSE, but that didn't do any thing.
So, how do I enable the CDialogBar object all the time?
-----------------------------------------------------------------------------------------
/* I can C */
// or !C
Yusuf
|
|
|
|
|
I have a dialog application with a handful of edit boxes. The
class CView1 is the class that handles the functions and variables
from this dialog. I have variables:
CEdit m_edit1;
and so forth.
I created some print functions in PrintForm.cpp/.h which prints
out some stuff. Id like to print out the variable m_edit1 in the
PrintForm.cpp functions, but Im just stumped. Rather than saving
the data and then retrieving it in PrintForm.cpp or making the
variables global, there has go to be a way to access m_edit1
directly. I keep getting:
ASSERT(::IsWindow(m_hWnd));
which tells me that the CView1 resource is not created. Is there
an easy way to resolve this?
Please, any response any one can give me will be greatly
appreciated.
Sincerely,
Danielle Brina (an overworked graduate student)
|
|
|
|
|
First off, the class CView1 will need to know about the dlg class so lets say the dlg class is called CMyDialog.
In your CView1 cpp file, you will need to include "mydialog.h"
/*
Note: When you do this with an SDI/MDI app, whenever you include the view, you will also need to include the doc, in that order because the view has some references to the doc that will generate errors if it can't figure out what the doc class is.
*/
Now in this class, you can call
void CView1::DoStuff()
{
CMyDialog* pWnd=(CMyDialog*)AfxGetMainWnd();
CString sEdit;
if (pWnd) {
pWnd->m_Edit1.GetWindowText(sEdit);
TRACE(sEdit);
}
}
/*
Remember that when you get asserts like that to make sure the window you are trying to get a pointer to exists. If you put this code in the constructor of CView1, for example, you will likely get asserts or errors of some kind.
*/
For other generic CWnd functions, you won't need the cast or the include.
Usually, any code that accesses the dialogs children is probably best placed in the dialog class to avoid dependent coupling like this. However, I hate it when people question "why" I'm doing this or that a certain way so as not to become a hypocrite, just consider that last statement a passing, friendly, reminder or helpful hint.;)
Take care
-- modified at 23:27 Wednesday 7th June, 2006
|
|
|
|
|
I'm writing a DLL that has to call a function after a certain event. However from all the articles I've read, you need to declare a function prototype inside the dll to be able to properly call the function.
Eg : typedef int (CALLBACK *someFunction)(const byte*, const byte*);
But what if I want to callback a function that has an arbitrary number of arguments, or return value?
Thanks.
|
|
|
|
|
I'm not sure to understand fully your question. You want to be able to call a callback without knowing in advance the number of parameters you need to send ? The only way of doing that is by using '...' in the argument list.
But this is really ugly. May I ask why you want to do that ? This looks like a bad design for me.
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
The reason I need to do this, is because my dll needs to support an arbitrary callback function.
The user would "register" the callback function with the dll, by passing a function pointer. However, I need to have a prototype inside the dll anyways to callback any function.
I was thinking I could make the user pass in the parameters, and number of parameters when the function is registered, then I could push it on the stack with inline assembly before I call the function.
However, this is a little bit tardy way of doing it.
Any better suggestions?
Thanks
|
|
|
|
|
vasanth1004 wrote: The reason I need to do this, is because my dll needs to support an arbitrary callback function.
Clearly, this is something that I don't understand. Why do you want to do such a thing ? The purpose of the callback is to notify the 'registered' that something happened and in every case, the prototype of your function is the same.
vasanth1004 wrote: I was thinking I could make the user pass in the parameters, and number of parameters when the function is registered, then I could push it on the stack with inline assembly before I call the function.
However, this is a little bit tardy way of doing it.
Yes, this is quite tricky and not elegant at all
vasanth1004 wrote: Any better suggestions?
Yes, change your design so you don't have to use different prototypes .
Maybe, if you describe the situation why you need to have an arbitrary function, I can help you to find a better solution. But please, you have to describe the situation so that I can fully understand the problem (and maybe some RELEVANT code snippet can help, altough it's not necessary).
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
Basically, I am writing an openGL library to render a scene multiple times from different viewpoints.
So, the way my design works is I have the user pass in a pointer to their "Draw()" function (callback method), and my dll can call the Draw function multiple times, with different configurations each time. This is done so that the user doesn't have to explicitly call the Draw function multiple times.
My DLL code can work fine if there were no arguments, or I knew the arguments.
eg: typedef void (__stdcall *Draw)(void);
But what if the user's Draw method had many arguments? How would I change the prototype in the dll to reflect this?
This is my problem.
Thanks for your help.
|
|
|
|
|
Ok I see. But basically, I think there is a design problem there. Ok, let's suppose you have several Draw functions with each a different parameter list. How, in your dll, will you know what parameters you will need to pass ? There is a big problem there. You simply cannot call a function without knowing what you will pass to it, this makes non-sense.
I think you need probably to rethink a little bit the design. As I see, it is the users that will use your dll (so you don't have control over this 'code'). Why not simply have a standard callback function ? It work always like that: you have a specific function prototype that can be used as a callback and it's up to the user to follow this prototype (that seems logical).
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
Is there an API or a way to retrieve or Calculate %CPU Usage and Thread Count of a specified process? I am using 2000/XP.
Thanks.
|
|
|
|
|
|
|
Hi friends
If i want to code a big project in C++ in VS2005 which road must i follow?What do you advice me?
Especially i want to design it with plugin support.I write it skeleton, then other parts will be plugin or it can be developed by other coders.I want to learn what must i prepare before start coding.How must projects settings be for plugin support or for a such a big project.(For great extensibility)
I am looking for your answers.Any links , articles, advices will be very useful for me.
Thanks..
|
|
|
|
|
the easiest way to do a plugin system is with plain-old stdcall/cdecl DLLs. just define an interface (a set of required functions) and document how a plugin should behave within your system. then you just drop the DLLs into a specific folder and your app can do LoadLibrary/GetProcAddr on each *.DLL it finds in that folder.
Cleek | Image Toolkits | Thumbnail maker
|
|
|
|
|
Thanks chris.
Do you know there are any sample applications about that.
Also are there also some information or settings that i must know before start coding?I asked this because i don't want to make mistake and turn back.
Thanks again...
|
|
|
|
|
hi
i can't think of any samples specifically related to "DLL as plugins", but i think just a simple introduction to DLLs using LodLibrary/GetProcAddr would do it. i'm sure there must be a bunch of those around.
i can't think of any special settings, really. just create your DLLs with C interfaces (not "MFC Extention" or COM or .Net), make sure you export the functions with C interfaces (stdcall or cdecl) and make sure you make your function interfaces as flexible as possible, to allow for easy growth (you don't want to be changing the interfaces once people have started writing plugins!).
Cleek | Image Toolkits | Thumbnail maker
|
|
|
|
|