|
Hello.
Are this all features those makes differentiated between Visual C++ 6.0 Standard and Visual C++ 6.0 Professional? Or are there still more differences?
- OLE DB Templates (new)
- Code optimization
- Profiler
- Static linking to the MFC Library
- Databound controls (RemoteData)
- InstallShield
- Custom AppWizard
- Visual Database Tools
|
|
|
|
|
Hi, everyone!
Suppose class foo have a member variable named "array" whose type is
an integer array. I wondered when the memory space of the array is allocated?
In my opinion, when creating an object of a class, the only action to do
is in ctor. Since the ctor does not allocate the space obviously, it really
puzzled me. Can anyone help?
Another question is, is it a correct way to initialize the array in ctor?
Any better solutions?
I have tried in my example that following statement results an error in VC 6.0.
--------
int array [100] = {1};
--------
So I wonder which is the best way to initialize the value of the array object.
Source Code:
--------
#include <iostream>
using namespace std;
class foo {
private:
int array [100];
public:
foo()
{
array [0] = 1;
}
void Dump()
{
cout << array [0] << endl;
}
};
int main()
{
foo f;
f.Dump(); //output is 1
return 1;
}
--------
Thanks in advance,
George
|
|
|
|
|
It is the constructor that will allocate memory.
However, you cannot initialize any member variable (array or otherwise) during declaration- you have to do it in a construcor.
AFAIK, your code is the only way to do it. Just out of curiosity, why do you want to do it otherwise- do you find this method awkward or something?
Vikram.
-----------------------------
My site due for a massive update
1. Don't ask unnecessary questions. You know what I mean?
2. Avoid redundancy at all costs.
3. Avoid redundancy at all costs.
"Do not give redundant error messages again and again." - A classmate of mine, while giving a class talk on error detection in compiler design.
|
|
|
|
|
Thanks, Vikram buddy!
As you said in your reply, "It is the constructor
that will allocate memory.", but I think in the ctor there
does not exist a statement which allocate the space for my
array. Can you show me the answer?
regards,
George
|
|
|
|
|
No, what I meant was that the constructor call and allocation of memory for the object go hand in hand.
You do not allocate space for the array explicitly. If you want to, you can- using a pointer. Like this:
class foo
{
int *p;//Pointer, but can be considered "array"
public:
foo()
{p=new int[1];}
};
Now you're ready to instantiate foo.
Hope this helps.
Any good book on C++ should be able to help you further. Try Schildt or Lafore. Even Bruce Eckel.
Vikram.
-----------------------------
My site due for a massive update
1. Don't ask unnecessary questions. You know what I mean?
2. Avoid redundancy at all costs.
3. Avoid redundancy at all costs.
"Do not give redundant error messages again and again." - A classmate of mine, while giving a class talk on error detection in compiler design.
|
|
|
|
|
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.
|
|
|
|