|
Thanks, Vikram buddy!
Have you ever read Tim's reply. I think he is correct. Which means
the ctor does not do memory allocation in my case.
Do you agree with him ( her ) ?
regards,
George
|
|
|
|
|
Say you have a class
class abc
{
private:
char a[100];
} As you would expect, when a new instance is created using new, 100 bytes of memory have to be allocated.
You write
abc* p = new abc();
which can also be written as:
void* px = malloc(sizeof(abc));
abc* p = new (px) abc();
So, the new allocates memory and then calls the constructor to initialize the class. In the second case, we explicitly allocate the memory and ask new to use the allocated memory to place the class.
The placement parameter in new is optional; but it decides whether malloc will be called, or the user already allocated memory.
Thomas
My article on a reference-counted smart pointer that supports polymorphic objects and raw pointers
modified 29-Aug-18 21:01pm.
|
|
|
|
|
Thanks, Thomas buddy!
You just covered the case of allocation on heap. But what about
the case of allocation on stack? Can you also show me an example?
(Since allocation on stack does not use "new" operator.)
regards,
George
|
|
|
|
|
|
In your example, the declaration of the variable or the usage of the "new" keyword will allocate the space for the variable. The ctor doesn't have anything to do with the allocation.
foo f; // allocates the variable on the stack
foo *pf = new foo; // allocates the variable in the heap
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Thanks, Tim buddy!
regards,
George
|
|
|
|
|
hi.
i had create a custom control and place it on my dialog based application.
may i noe:
(1) how can i remove that custom control during run-time (that is, after the dialog based application had shown up and drawn the custom control on itself).
(2) how can i disable message x received by that custom control during run-time. (initially, message x can be received and responded by custom control by specified in BEGIN_MESSAGE_MAP block)
thank you.
regards
yccheok
|
|
|
|
|
One solution is EnableWindow() and ShowWindow().
Kuphryn
|
|
|
|
|
hi folks.
is there an addin that integrates to the VC6 IDE for RAD development, like:
- C++ Builder
- Visual Basic
- Delphi
- ...
i know one addin that do what i mean. called RadVC. but i cant use this addin, it crashes only on my system. anyone know a good RAD addin?
thank you
|
|
|
|
|
I've observed that many good C++ code writers, makes the global functions of their progam as static . What is the reason behind it? Similarly in his book, Programming Windows, Petzold, used a characted array globally and made it static.
|
|
|
|
|
static makes the function etc. only visible within the file it is in. Therefore it is minimizes name space pollution ie. It isn't visible outside of the current file. That said global objects and functions should be avoided wherever possible.
Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
|
|
|
|
|
Hi there, i'm having trouble with some c++ code, regarding classes and operators. Given the following code:
class TestInt {
private:
int _value;
public:
TestInt() : _value(0) {}
TestInt(int newValue) {
TestInt();
_value = newValue;
}
~TestInt() {}
operator int () {
int aux;
aux = _value;
return aux;
}
void operator = (int newValue) {
_value = newValue;
}
};
and
int main() {
TestInt t = 0;
TestInt x = 0;
TestInt y = 0;
int i = 0;
t = 50;
x = 100;
y = 300;
printf("i is %d, t is %d, x is %d, y is %d\n", i, t, x, y);
i = t;
t = 60;
t = x;
printf("i is %d, t is %d, x is %d, y is %d\n", i, t, x, y);
return 0;
}
I think it's supposed to output
i is 0, t is 50, x is 100, y is 300
i is 50, t is 100, x is 100, y is 300
but instead, it outputs
i is 0, t is 50, x is 2081702084, y is 88
i is 50, t is 100, x is 2081702084, y is 88
Can someone point out what's wrong with the posted code? oh, by the way, yes... operator int() really does need the int aux local variable because this code is only a simple example and yet, does not work. These weird results look like somethin's happening in the stack that shouldn't be.
thanks in advance for the help,
Rui
is this a sig?
|
|
|
|
|
<br />
TestInt(int newValue) <br />
{ <br />
TestInt();<br />
_value = newValue; <br />
}<br />
Should be
<br />
TestInt(int newValue) <br />
: _value(0)<br />
{ <br />
_value = newValue; <br />
} <br />
C++ doesn't support the notion of 'delegating constructors', I'm guessing you're familiar with java. In C++ you have to initialise all the members that need to be initialised in each constructor. A good artical on this topic was published in a recent C/C++ users journal
Delegating Constructors? (by Herb Sutter &Jim Hyslop),
C/C++ Users Journal, 21(5), May 2003.
|
|
|
|
|
I've changed the code as you've said but sadly it still doesn't work. I'm using VC++ 7, does it help saying that?
And here is the copy+paste from the output after the changes:
i is 0, t is 50, x is 2081702084, y is 88
i is 50, t is 100, x is 2081702084, y is 88
I think the problem is somewhere how the printf function call handles the class value, but in some vars the value is right, on other vars the value is completely absurd.
Anyone could help in the solution?
Thanks,
Rui
is this a sig?
|
|
|
|
|
Sorry, my fault, didn't read it closly enough. printf uses va_args, and the conversion operator may not be called.
You have two choices:
1/ explicitly extract the int you want
2/ use standard output stream, they have to use your conversion operator, because they have no other choice.
cout << "i is " << i << " t is " << t << " x is " << x << " y is " << y << endl;
Instead of using printf
|
|
|
|
|
Thanks, it works
Isn't there a way to enforce printf using the conversion operator, not using explicit extraction?
Thanks again,
Rui
is this a sig?
|
|
|
|
|
There is, but using standard output streams is much better practice in c++, it allows you to be far more flexible and typesafe. If you insist on using printf you could try:
1/ changing the conversion operator to a getValue method.
2/ put an explicit cast within the printf statement.
|
|
|
|
|
How do you declare the variables and associate them with my controls using class wizard? I have set up all my controls but when I look in the members variables tab, it's empty. Do I need to do something else?
|
|
|
|
|
Right-click on the control and select add variable. Here is an example of a button.
DDX_Control(pDX, IDC_BUTTONX, m_BtnX);
Kuphryn
|
|
|
|
|
I did right-click and nothing that mentions variable comes up in the list. Did I mentioned that I have c++ 6.0? I wish I could show you a picture of what I'm doing but I don't know how to upload here plus I'm not a member. Maybe I can post it on my site and link it? If you need it.
|
|
|
|
|
There are three basic components of any control. First is the member variable declaration. This defines a member variable of the type of your control. Second is the data distribution that occurs between your control and the member variable (as mentioned above). Third is message mapping. Controls fire messages to your application whenever certain things occur (i.e. u click the mouse, etc...) When you want to do something on these events, you are "handling" the messagess. Apart from threading, this is the heart of your application.
- Nitron
"Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb
|
|
|
|
|
Hi,
I have two class in wich I put member function. I want to pass a pointer to member function from one class to another class member function. I utilize Visual C++ 6.0 and MFC.
Here's the class definition:
class A {
...
public:
void TestA ( int x);
...
};
class B {
...
public:
void TestB( void(*ptr) ( int ));
...
};
Here's is the member function A::TestA ( int x) from wich I want to pass a pointer to the member function B:: TestB( void(*ptr) ( int )).
void A::TestA ( int x)
{
// Definition of the pointer to member function
void (A::*pt) ( int );
// Assignment
pt = &A::TestA;
// Now, I want to pass the member function A::TestA ( int x) address to the member function TestB of class B:
B objB;
objB.TestB ( pt );
}
This is not compiling in Visual C++.
I get a compile error who explain that the calling convention __cdecl does not correspond.
I have read many web site about this subject. Unfortunately I am not able to get the right code to correct my error.
I have read about callback and functor, but I can't understand.
Do you have any help ?
Thanks,
Claude Gagnon
|
|
|
|
|
A pointer to a member function is not interchangeable with a pointer to a global function, because the former also has a this pointer. You can see it in the code, void(*)(int) versus void(A::*)(int)
One way to fix it is to have TestB() declared as void TestB( void(A::*ptr) ( int )) although without knowing what your whole app is like I don't know if this the best way.
--Mike--
Latest blog entry: *drool* (Alyson) [May 10]
Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber
"You have Erica on the brain" - Jon Sagara to me
|
|
|
|
|
How can we enter an integer data in a database?
My database is SQL
When I try to do this I see an error
rose:
|
|
|
|
|
Hello,
how can I load a gif file into the display
device context? I don't want to use a resource.
Thanks
|
|
|
|