|
Hi,
1) What is the difference between the following two declations:
a) *objectPtr->leftPtr = *smallObject->leftPtr
b) objectPtr->leftPtr = smallObjectPtr->leftPtr
In an expression like int* pVal = &Val; you always have 2 involved objects:
- the 'real' object (here: int Val (defined anywhere else))
- the pointer (here: int* pVal )
So, 1a) will copy the content of your 'real' objects as follows:
objectPtr->leftPtr->memberA = smallObject->leftPtr->memberA;
objectPtr->leftPtr->memberB = smallObject->leftPtr->memberB;
objectPtr->leftPtr->memberC = smallObject->leftPtr->memberC;
If OtherObject has a Copy-Constructor 1a) will call it.
And 1b) will copy the content of your pointers (the numerical adress values).
2) How can I tell which one to use?
Depends on what you want to do:
- if you want to copy the content of your object, use 1a)
- if you want to have two pointers pointing at the same object, use 1b)
3) Finally, is there anything wrong with the following declaration and what does it mean (or vice versa regarding the "*" dereference):
Your VC++ compiler should give you a C2440 error.
This line says: OtherObject* = OtherObject . It's like int* = int . Obviously this can't work.
Regards
Achim Klein
We can do no great things, only small things with great love. - Mother Theresa
|
|
|
|
|
Thanks Achim Klein. I appreciate you taking the time to respond. That's more good information.
|
|
|
|
|
Achim Klein wrote:
If OtherObject has a Copy-Constructor 1a) will call it.
Naaah. I suppose you mean copy assignment operator. This invokes the copy constructor: A secondA = firstA if firstA is of type A .
--
The Blog: Bits and Pieces
|
|
|
|
|
Hi Johann,
you're absolutely right. The statement given in 1a) just calls the assignment operator OtherObject& operator=(const OtherObject& Obj) if there is one.
Sorry for that mistake.
Regards
Achim Klein
We can do no great things, only small things with great love. - Mother Theresa
|
|
|
|
|
The basic thing to remember about a pointer of type A* is that it either points at nothing (NULL ) or to a memory address where a chunk of memory resides which size is an even multiple of sizeof(A) .
Imagine a part of the memory space, where an A[4] array resides and a pointer A* pA which points at the first of these A s:
A chunkA[4];
A* pA = A; And this looks like:
| | | | | | |A|A|A|A| | | | | | | |
|_|_|_|_|_|_|0|1|2|3|_|_|_|_|_|_|_|
^
|
|------------- pA Now we can say
A a0 = *pA++;
A a1 = *pA++;
A a2 = *pA++;
A a3 = *pA; Stepping pA once more would take it outside the A[4]. Since we don't know what is there, we don't want to dereference pA if it points there, e.g.:
A a3 = *pA++
A a4 = *pA; have small chances of resulting in expected behavior...
The expressions (a) - (d) will all invoke the copy constructor of A, which in the default case will invoke the copy constructor for all its members that have a copy constructor and make a binary copy of pod mebers that lack copy constructors. Ad infinitum. Sort of.
--
The Blog: Bits and Pieces
|
|
|
|
|
I have a .Net Managed C++ wrapper control for a MFC control. When using the
.Net control in a Visual Studio 2005 Beta 2 C# project and running in Debug
mode, I get a crash saying something like "First Chance Exception : memory
cannot be read". I found that this was becuase a call to AfxGetThread() was
returning NULL. I tried using AFX_MANAGE_STATE(AfxGetStaticModuleState());
but it did not solve the problem.
Furthermore, this crash only occurs in Debug Mode and not in Release Mode.
Also, this occurs only in VS.Net 2005 Beta 2 and not in VS.Net 2003 ( either
release or debug)
How can this be resolved?
|
|
|
|
|
Hi All,
I am calling a pure virtual function in a constructor and the linker is complaining that there is no function definition. The scenario is this:
class base
{
base (void) { f1 (); }
virtual void f1 (void) = 0;
}
class child : public base
{
child () { }
void f1 () { cout << "This should print" << endl; }
}
The linker complains that there is no f1 function defined. If I move the call to another function and call f1 then there is no problem.
Does anyone know a way around this?
regards,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proff programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
In C++, you shouldn't call virtual functions from the constructor or the destructor. The derived class's functions are updated in the virtual table only when their constructor runs. The base class's constructor runs before the derived class's, so during execution of the base class constructor, only the base class implementation of the virtual functions will be available.
In your case, it happens to be pure virtual and so the linker is complaining. The only way I know around this is to call a base class function (equivalent to the constructor) from every derived class's constructor. Ugly and I'm not sure it's standard C++ behavior, but it works because at that point the derived class has already set up the vtable for it's overridden virtual functions.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Thanks for the info. I had always wondered about the v-table that appears in the VC debugger.
regards,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proff programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
|
Thanks for the site, some good info to know
regards,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proff programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
Hello,
I want to know how I can convert ASCII strings into their Hex Values. For example:
A = 41
K = 4B
I'm programming with Dev-C++ if this is any help.
With best regards,
Benedikt
|
|
|
|
|
Hi there,
This might sound silly but you just need to cast them to an int e.g.:
const char string[] = "test string";
for (int i = 0; string[i] != '\0'; ++i)
printf ("Hex 0x%x\n", (int) string[i]);
regards,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proff programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
I think he meant converting the string representations of a hex value to the actual hex number, not the hex of any character value.
--
The Blog: Bits and Pieces
|
|
|
|
|
Oh, try this then:
const char string[] = "test string";
char hexstring[200];
hexstring[0] = '\0';
for (int i = 0; string[i] != '\0'; ++i)
sprintf (hexstring, "%s %x", hexstring, (int) string[i]);
This will create a hex list of all the characters in string .
regards,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proff programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
...still not quite there
I'm quite sure he meant this that he has a string "D4", which without the quotes is the hex number D4. He wants to get the hex number D4, not the hex for the value of the characters 'D' or '4'. There are lots of ways to do this. I'll punch a couple out later tonight when I have time to concentrate better.
--
The Blog: Bits and Pieces
|
|
|
|
|
Can anyone plz help with the implementation of the following member functions.
1.void EmployeeDatabase::getEids(vector <int> &ids)
2.string EmployeeDatabase::getName(int employeeID)
3.double EmployeeDatabase::getShoeSize(int employeeID)
<code>
#include "EmployeeDatabase.h"
#include<vector>
using namespace std;
EmployeeDatabase::EmployeeDatabase()
{
size = 0;
}
/*void EmployeeDatabase::getEids(vector <int> &ids)
{ // Complete the code.
//int iter;
//iter = v.assign(ids, people);
for(int i = 0; i<=ids.size( ); i++)
{
}
return ids;
}
*/
/*string EmployeeDatabase::getName(int employeeID)
{// Complete the code.
// Returns the name of the Person with eid equal to employeeID.
//string TheEmployees;
}*/
double EmployeeDatabase::getShoeSize(int employeeID)
{// Complete the code.
// Returns the shoesize of the Person with eid equal to employeeID.
}
void EmployeeDatabase::makePerson(string employeeName, int employeeID, double employeeShoeSize)
{
vector <Person> m_Staff;
Person people(employeeName, employeeID, employeeShoeSize); // this assumes a class called person with this constructor signature
m_Staff.push_back(people);
}
int EmployeeDatabase::getSize()
{
return size;
}
</code>
here is the application test program I have to test the member functions.
<code>
#include "Person.h"
#include "EmployeeDatabase.h"
#include "Data.h"
#include<iostream>
#include<fstream>
using namespace std;
int main(){
EmployeeDatabase TheEmployees; // These two commands make the data in the database Data.o available
Data DB(TheEmployees); // Include these two commands at the beginning of your program, and
// then use the methods from EmployeeDatabase to get the data.
vector<int> ids;
TheEmployees.getEids(ids);
cout << "The Eid of the first employee is " << ids[0] << '\n';
cout << "The name of the first employee is " << TheEmployees.getName(ids[0]) << '\n';
cout << "The shoe size of the first employee is " << TheEmployees.getShoeSize(ids[0]) << '\n';
pause();
}
</code>
|
|
|
|
|
|
i wrote some program abt file reading and writing
in that
i declare one oftream object for writing . and i do like this
ofstream obj1;
obj1.open("......");
........;
......;//some program for writing
obj1.close();
obj1.open(".........");//another file
........;
......;//some program for wrtiing
obj1.close();
that works fine.
problem is if i declare one item for reading as writing it is not reading another file
ifstream obj2;
obj2.open("......");
........;
......;//soem progrma for readin
obj2.close();
obj1.open(".........");//it` unable to openfile
ifstream obj3;
obj3.open("......");//it`s working fine
........;
......;//some program for reading
obj3.close();
but in another program abt reading files i just declare one object for reading two files and it works fine(but that program for reading contents from existin files only)
note: in program i deal with .doc files
Regards
cnu...
-- modified at 22:22 Thursday 15th September, 2005
|
|
|
|
|
Hi everybody!
I am trying to make a graphic application in linux.In the beginning of my .cpp file, I have:
#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <math.h>
#include "/home/hungnv/Tools/Rand.h"
#include "/home/hungnv/Tools/nrutil.c"
#include "/home/hungnv/Tools/nrutil.h"
#include "/home/hungnv/Tools/XRSver1.h"
#ifndef _NO_NAMESPACE
using namespace std;
using namespace math;
#define STD std
#else
#define STD
#endif
#ifndef _NO_TEMPLATE
typedef matrix<double> Matrix;
#else
typedef matrix Matrix;
#endif
#ifndef _NO_EXCEPTION
# define TRYBEGIN() try {
# define CATCHERROR() } catch (const STD::exception& e) { \
char Error[1000];\
sprintf(Error,"Error: %s\n",e.what());\
if(flog != NULL){fprintf(flog,Error);} }
#else
# define TRYBEGIN()
# define CATCHERROR()
#endif
when I compiled by using this command: gcc -O -o xxx xxx.cpp -L/usr/X11R6/lib -lm -lX11 -I/usr/X11R6/include
and some errors occured. It could not recognize "Matrix".
Please help me to fix this problem.
Thanks!
h
|
|
|
|
|
1. We can't see the stuff in <> because you didn't check Do not treat <'s as HTML tags
2. Why do you expect it to recognise Matrix ?
3. Why do you define STD ? The only reason I can think of is to put STD:: in front of methods, but you've put using namespace std;, which makes this redundant
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
1. Please post here instead of via email
2. Please check the box below the text entry window so that we can see the code that uses <> brackets.
3. It may help to post the actual error
4. It's obvious that you expect your includes to provide you with a matrix class. As such, posting all your code is unlikely to help, the problem is that it can't see this class, for whatever reason. Does any of these header files define a class called Matrix ? I assume the #ifdef is to differentiate between a templated and non-templated class. Are you copying this code from somewhere ? It looks like it's being written for use with crappy compilers that are no-where near standard C++. Why not just code for your compiler ?
Also, if you're including iostream.h and not iostream ( etc ), then the using namespace std stuff is useless, and if you're including iostream (etc), then you're already assuming your compiler supports namespaces.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
In the C++ STL is there a defined class/method or feature to convert/cast a structure of C-style character arrays to a structure of std::strings?
Sorry if this is in the archives but I did search before posting...
|
|
|
|
|
The string class takes a char array to it's constructor I believe. How are you wanting to do this ? There's no method to take an unknown struct and convert it to another unknown struct ( that is, from one struct the STL knows nothing about to another ).
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
My program is passed a pointer to a data-filled structure for which all elements are char arrays...I am looking for a way to change the type of the buffer or copy the buffer to so that I can use C++ features to work on the fields of the new buffer.
from C-style:
struct Carrays
{
char field1[50];
...
char field100[25];
} Carrays_t;
to C++ style:
struct Strings
{
std::string field1;
...
std::string field100;
}
|
|
|
|