Click here to Skip to main content
15,888,313 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: problem with setwindowtext() Pin
Hamid_RT7-Jun-06 20:09
Hamid_RT7-Jun-06 20:09 
GeneralRe: problem with setwindowtext() Pin
swatgodjr7-Jun-06 20:27
swatgodjr7-Jun-06 20:27 
QuestionSTL sort problem Pin
_anil_7-Jun-06 15:56
_anil_7-Jun-06 15:56 
AnswerRe: STL sort problem Pin
Christian Graus7-Jun-06 16:16
protectorChristian Graus7-Jun-06 16:16 
GeneralRe: STL sort problem [modified] Pin
_anil_7-Jun-06 16:47
_anil_7-Jun-06 16:47 
GeneralRe: STL sort problem [modified] Pin
Christian Graus7-Jun-06 16:59
protectorChristian Graus7-Jun-06 16:59 
GeneralRe: STL sort problem [modified] Pin
_anil_7-Jun-06 17:06
_anil_7-Jun-06 17:06 
QuestionReturning a reference... check my work, please Pin
MALDATA7-Jun-06 15:40
MALDATA7-Jun-06 15:40 
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
AnswerRe: Returning a reference... check my work, please Pin
Christian Graus7-Jun-06 16:19
protectorChristian Graus7-Jun-06 16:19 
GeneralRe: Returning a reference... check my work, please Pin
MALDATA7-Jun-06 18:12
MALDATA7-Jun-06 18:12 
GeneralRe: Returning a reference... check my work, please Pin
Michael Dunn7-Jun-06 19:28
sitebuilderMichael Dunn7-Jun-06 19:28 
Questionany body knows structures??? Pin
Inder gujral7-Jun-06 15:17
Inder gujral7-Jun-06 15:17 
AnswerRe: any body knows structures??? Pin
Christian Graus7-Jun-06 17:01
protectorChristian Graus7-Jun-06 17:01 
GeneralRe: any body knows structures??? Pin
bob169727-Jun-06 18:31
bob169727-Jun-06 18:31 
AnswerRe: any body knows structures??? Pin
Michael Dunn7-Jun-06 19:29
sitebuilderMichael Dunn7-Jun-06 19:29 
GeneralRe: any body knows structures??? Pin
NiceNaidu7-Jun-06 19:59
NiceNaidu7-Jun-06 19:59 
QuestionHow to enable CDialogBar always? Pin
Yusuf7-Jun-06 14:53
Yusuf7-Jun-06 14:53 
QuestionASSERT(::IsWindow(m_hWnd)); Pin
DanYELL7-Jun-06 14:01
DanYELL7-Jun-06 14:01 
AnswerRe: ASSERT(::IsWindow(m_hWnd)); [modified] Pin
bob169727-Jun-06 17:26
bob169727-Jun-06 17:26 
QuestionCallback function arguments Pin
vasanth10047-Jun-06 12:42
vasanth10047-Jun-06 12:42 
AnswerRe: Callback function arguments Pin
Cedric Moonen7-Jun-06 20:20
Cedric Moonen7-Jun-06 20:20 
GeneralRe: Callback function arguments Pin
vasanth10048-Jun-06 3:51
vasanth10048-Jun-06 3:51 
GeneralRe: Callback function arguments Pin
Cedric Moonen8-Jun-06 3:58
Cedric Moonen8-Jun-06 3:58 
GeneralRe: Callback function arguments Pin
vasanth10048-Jun-06 4:45
vasanth10048-Jun-06 4:45 
GeneralRe: Callback function arguments Pin
Cedric Moonen8-Jun-06 4:57
Cedric Moonen8-Jun-06 4:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.