|
Hi.
I have two classes, CMyFirstClass and CMySecondClass.
MyFirstClass creates CMySecondClass objects dynamically. It is NEVER appropriate to create a CMySecondClass outside of the CMyFirstClass object.
To make things tidier, i put the declaration of CMySecondClass inside of CMyFirstClass. Now I have a shorter list in Class View. And my app still works.
My question: Is this a good idea? is there a compelling reason NOT to nest class declarations?
Jon
Sorry to dissapoint you all with my lack of a witty or poignant signature.
|
|
|
|
|
Hi,
Nested classes are quite heavy used in MFC, If I´m not wrong ( or if the things haven´t change this times ), for example is how MFC creates COM objects ( the macros for the interfaces, declare just nested classes), so I think it´s ok.
HTH
Braulio
|
|
|
|
|
Jon Hulatt wrote:
Is this a good idea?
why not?
Mazy
"So,so you think you can tell,
Heaven from Hell,
Blue skies from pain,...
How I wish,how I wish you were here." Wish You Were Here-Pink Floyd-1975
|
|
|
|
|
I think it is more a style thing. Unless it is a very small class utility class I don't like to nest classes. Makes it harder to read otherwise. Just my opinion though.
|
|
|
|
|
You don't prevent third code from creating CMySecondCLass es just by defining it into CMyFirstClass . It is required also that:- The nested class lies in the private part of the nesting class,
- or the constructors for the nested class are private and the nesting class is a friend of the nested class
Apart from this, nesting it is an excellent way to restrict visiblity and show design dependencies between classes (IMHO).
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
The declaration of the second class is within a protected: section.
Doesn't really bother me if technically i could create an instance of the second class elsewhere, it's definately more of a design and organisation thing.
I just wondered if i was overlooking some limitation or side effect of class nesting.
Obviously not, great!
Sorry to dissapoint you all with my lack of a witty or poignant signature.
|
|
|
|
|
|
thanks nish. although it's got to be a class, because it has member functions and things. besides, what's the difference between a struct anyway?
And I like the whole nested classes thing. it makes things more confusing for the uninitiated. Which makes me feel cleverer.
Does anyone else here secretly enjoy it when they come up with something really complicated that noone else will ever make head or tail of?
Sorry to dissapoint you all with my lack of a witty or poignant signature.
|
|
|
|
|
Does anyone else here secretly enjoy it when they come up with something really complicated that noone else will ever make head or tail of?
No, because every time I do something like that, I am the one who can't make heads or tails out of it 2 months later.
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|
|
A struct and a class are basically the same thing in c++, however a struct defaults all of its members to public and the classes default declaration is private.
Other than that you can inherit and do all of the things that you can do from a class, with a struct.
|
|
|
|
|
Jon Hulatt wrote:
Does anyone else here secretly enjoy it when they come up with something really complicated that noone else will ever make head or tail of?
I enjoy it, but I also enjoy trying to explain to someone what I actually did, and try to help them understand it, then they get a deeper appreciation for some of the code that I develop.
|
|
|
|
|
Jon Hulatt wrote:
Does anyone else here secretly enjoy it when they come up with something really complicated that noone else will ever make head or tail of?
I prefer hearing that my code and interfaces are clear and easy to understand. Code that is extremely hard to understand is usually an indication of poor design.
/ravi
"There is always one more bug..."
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
Theres nothing wrong with nested classes. If CMySecondClass is only meaningful in the context of a CMyFirstClass object, then nesting the classes makes this clear.
However, if the second class is an implementation detail that doesn't appear in the public interface, there is another technique that you may be able to apply. In the header file that declares CMyFirstClass you simply give a forward reference to CMySecondClass:
class CMySecondClass;
class CMyFirstClass
{
public:
protected:
CMySecondClass *m_p_instance;
};
Now, both the declaration and implementation of CMySecondClass can be put in the implementation file
for CMyFirstClass. This provides much stronger encapsulation than a nested protected or private class, since the details of CMySecondClass aren't even visible to code that uses CMyFirstClass (other than the fact that CMySecondClass exists). With a nested protected or private class, the details of CMySecondClass are visible, but access is restricted.
Stephen C. Steel
Kerr Vayne Systems Ltd.
|
|
|
|
|
I have imported a project developed in VC++ 6.0.
and am getting this(these) errors when running
C:\\windows\system32\ntdll.dll , cannot find or open a required DBG file
C:\\windows\system32\mfc70.dll , cannot find or open a required DBG file
PS. It compiles and is able to run but what do this errors mean? I tried search MSDN but nothing.
How do I fix these?
Thanks -JoeSox
|
|
|
|
|
Try issuing a "Rebuild all" command. It could be that some .obj has not been properly refreshed.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Tried that, still getting same output in debug window.
I guess I shouldn't worry about it if my app is working ok.
|
|
|
|
|
Those are just information messages issued by the debugger. All it is saying is that it can't find a valid DBG/PDB file for the specified DLL. Since those DLLs are system DLLs, it isn't shocking that they can't be found.
Very very very very safe to ignore.
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|
|
Tim, Can I ask a follow-up question?
In VC++ 6.0, I would hit the run button, it would ask me to compile, and it would only compile the parts I just modified.
It seems in VC++.NET, I hit the run button and it compiles ALL the Classes, which takes longer. How do I change this setting?
|
|
|
|
|
I know. I hate that too. I wish I knew what the setting was. I also hate that if your top document is from X.DLL, then only X.DLL gets built when you hit build. I liked the old V6 system where it would build your current project. This whole 'program to start' setting is confusing.
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|
|
If it wasn't for the better handling of the toolbars (support for over 256 colors) I wouldn't be trying to use VC++.NET at all.
I have alot of bugs in my inmported code I must now fix.
It would take me too long to rewrite all my code.
|
|
|
|
|
I want to work with a database that has a large number of records,So I want
to read the database,and in disconnect mode,modify,add or delete in database,after all
of them update the datasource.which class or object should I use?
Mazy
"So,so you think you can tell,
Heaven from Hell,
Blue skies from pain,...
How I wish,how I wish you were here." Wish You Were Here-Pink Floyd-1975
|
|
|
|
|
Hi,
I have many forms that would share many stuff (function, variables) and I'd like to create sort of a template class with CFormView, so that my forms could derrive from CFrmTmpl instead of CFormView. Here's some code:
class CFrmTmpl : public CFormView
{
protected:
CFrmTmpl(); // protected constructor used by dynamic creation
DECLARE_DYNCREATE(CFrmTmpl)
// Form Data
public:
//{{AFX_DATA(CFrmTmpl)
enum { IDD = _UNKNOWN_RESOURCE_ID_ };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
**************
class CFrm1 : public CFrmTmpl
{
protected:
CFrm1(); // protected constructor used by dynamic creation
DECLARE_DYNCREATE(CFrm1)
// Form Data
public:
//{{AFX_DATA(CFrm1)
enum { IDD = IDD_DIALOG1 };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
also:
CFrmTmpl::CFrmTmpl()
: CFormView(CFrmTmpl::IDD)
{
********************
CFrm1::CFrm1()
: CFrm1(CFrm1::IDD) // I know this does not work, that's the prob!
{
The problem that I have is the IDD and the constructor. Seems like the CFormView absolutely needs an IDD. Is there a way to make this work?
Thanks!
---------------
http://www.edovia.com
|
|
|
|
|
I guess that the best way is not to derrive from CFormView. Just create your own class and instanciate it in every form...
thanks Luke!
---------------
http://www.edovia.com
|
|
|
|
|
No problemo Luke!
---------------
http://www.edovia.com
|
|
|
|
|
Follow this scheme:
class CFrmTmpl : public CFormView
{
protected:
CFrmTmpl();
CFrmTmpl(UINT uId);
...
);
CFrmTmpl::CFrmTmpl(UINT uId)
: CFormView(uId)
{
...
}
CFrm1::CFrm1()
: CFrmTmpl(CFrm1::IDD)
{
...
) The idea is to have an additional constructor in CFrmTmpl that accepts the IDD from its derived classes. In this scheme, the default ctor for CFrmTmpl is never used, though I think DECLARE_DYNCREATE(CFrmTmpl) requires it.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|