"MFC is not a language; it is a native (unmanaged) class library which is a thin wrapper around the Win32 API."
now see these header function:

void CMyAxUICtrl::OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& /*rcInvalid*/)

i belive the CMyAxUICtrl is the instance\object name but it's used outside of main function: can i do these in C++?
(like you see: the OnDraw is used, but imagine that the programmer don't use it)
(sorry if my question is confused... but i'm trying)

Recommended Answers

All 25 Replies

Not quite. CMyAxUICtrl is the name of a class. To use this function, you would have to create an object of the class type first:

CMyAxUICtrl beansOnToast;

and then use the function in that object

beansOnToast.OnDraw(param1, param2, param3);

If I had to guess, I'd say that OnDraw is probably called automatically under various conditions (functions that involve drawing often are).

If this is MFC, there are probably restrictions on how you can create it and what the constructor is and all that sort of thing, but the principle is the same. To use a non-static class function (which this is), you have to create an object of that class, and then call the function in that object.

commented: thanks for correct me +2

CMyAxUICtrl is a class, not an instance of a class.

but it's used outside of main function:

Do you mean it's used in some other function? main() has nothing to do with it.

Looks to me what you posted is the implementation for OnDraw(). That's just normal c++.

I wouldn't bother learning much MFC -- it's pretty much obsolete now. New progarams will most likely be written in C# or C++/CLR.

my objective is understand that function header, for do the same in C++ ;)

sorry if i mistake something.. now i remember the MFC(98) that i used, with windows messages, i created a new class.
but how these class are done?
because we use what messages we need and not all in class

The scoping operator (::) is a standard part of C++, not anything specific to MFC. It has three main uses:

  • It indicates the membership of a method to its class, when the function is implemented outside of the class body (which is the usual case). For example, you could have a header with the following:

     class Foo
     {
     private:
         int quux;
    
     public:
         void bar(int x);
         static void baz(Foo neg);
     };
    

    The function implementations would then go in a separate source file.

     void Foo::bar(int x)
     {
         quux *= x; // set quux to quux * x
     }    
    
     static void Foo:baz(Foo neg)
     {
         neg.quux = - neg.quux;
     }
    

    The main purpose of this is to allow the separation of declaration and implementation. Since putting the implementation of any substantial function in a header is a Bad Thing, this is quite important.

  • It is used to indicate class membership for static functions and variables. For example, given the case above, you would call baz() like so:

    Foo::baz(f);
    
  • Finally, it is used to indicate that the function, class, or variable is inside of a namespace. For example,

    std::cout << "Hello, World!" << std::endl;
    

    or

    using std::cout;
    

    This is used to resolve namespace conflicts.

but how can i create a class dirived from other, without redeclare the functions?

Simple -- just inherit the base class

class Base
{
   ....
};

class Derived : public Base
{

};

Everything that's in Base class is now available ikn Derived. Just normal c++.

but how these class are done?
because we use what messages we need and not all in class

In MFC it's very complicated, there are many levels of inherentence. Here is a hierarcy chart of MFC classes. Most MFC classes use CObject as the base class. Then visual objects are derived from CView, so you get something like this:

CObject --> CView --> CButton 
                  --> CCheckbox
                  --> CComboBox
                  --> CMenu
                  ,,,

MFC uses a system of message notifications to pass messages around the system. When you press a key the keypess event is sent to the current active window. Same with mouse clicks. Many other events are generated by MS-Windows operating system and sent to the active application program. When your MFC programs receives a message it determines which MFC object should get it and passes the message off to it. This whole process is pretty complicated but you can via VC++ debugger trace through that process (yes, I've done that in order to gain understanding of what is going on).

#include <iostream>

using namespace std;

class test
{
    public:
        void print(string message);
};

class a:test
{
};

void a::print(string message)
{
    printf("hello %s",message);
}

error message: "no 'void a::print(std::string)' member function declared in class 'a'"

how can i resolve these problem?
like you see in class 'a':
1 - is derived from class 'test';
2 - the function isn't redeclared;
3 - the function is changed outside of class 'test'.

The function is still in test class, not a class.

void test::print(string message)

line 11 is not quite right either

class a:public test
{
public:
   a() { print("Hello from Class a\n") };
}

the function is changed outside of class 'test'.

The function can not be "changed", but it can be "called" as in my example above. Well, that's not entirely true either, which you will learn when you get to more advanced c++ topics.

yes.. the line 11 was my mistake.. sorry.
i continue confuse on how can i resolve that problem :(
like we have seen in MFC, it's possible be done, but i need more information :(

Resolve what problem? What do you need more info about?

sorry i understand that isn't the 1st time that i ask... and you always helped me... thanks for all.

but i continue thinking that something like these is possible with C++:

class test
{
    public:
        virtual void print(string message);
};

class a: public test
{
};

void a::print(string message)
{
    printf("hello %s",message);
}

but the problem is the print() not been re-declared in class 'a'... but MFC make these possible :(
so how can i resolve these problem?
(sorry, if i'm bored you with these problem. but i need do something like these)

virtual implies that the function can be overridden by some derived class. The reason you might do that is to implement the function differently in the derived class then it is in the base class. Here is a simple example

class Animal
{
public:
   virtual void Speak()
   {
      cout << "Wolf!\n";
    }
};


class Cat : public Animal
{
  virtual void Speak()
  {
     cout << "Meow!\n");
  }
};

class Cow  public Animal
{
  virtual void Speak()
  {
     cout << "Moo!\n");
  }
};

Notice that each derived class, Cat and Cow, change the behavior of function Speak. If the derived class does not override Speak then the base class Speak function is called.

In your example class a does not override print() from the base class, so you can't have a::print(), it must be test::print().

i only see 1 'problem'(for my objective) in Cow and Cat. why? because i must re-declare the Speak() function.

class Cow : public Animal
{

};

void Cow::Speak()
{
    cout << "Moo!\n");
}

:( i can't do these code, because of these error:
Speak isn't a member of Cow :(

That's right -- your Cow class has no member functions. Compare what you posted with what I posted. You can't just simply remove Speak() out of the class like you did.

but we can do something like that with MFC, right?
(i'm less than bigginer with MFC.. so sorry if i fail in something)
how can i validate the Speak() function without re-declare it inside of Cow class?

how can i validate the Speak() function without re-declare it inside of Cow class?

The only way I can think of is to make the base class Speak() function non-virtual, so that every derived class inherits the same func.

class Animal
{
public:
    void Speak()
    {
        cout << "Generic animal noise!\n";
    }
};


class Cat : public Animal
{

};

class Cow:  public Animal
{

};

//

Cow cow;
cow.speak();

sorry, but when i said"without re-declare it inside of Cow class", i mean avoid these line:

class Cow : public Animal
{
    void Speak(); //i need avoid these line
};

void Cow::Speak()
{
    cout << "Moo!\n");
}

but we can do something like that with MFC, right?

Wrong. MFC is just c++. If you can't do it in c++ then you can't do it in MFC. Show us an examplem of where MFC avoids redeclaring a functionl.

so tell me, if you can:

how MFC do for use these 'events':

void classname::functionname(...)
????
(like i said, i'm less than bigginer with MFC. but i remember that i must create 1 class for use the windows messages, but i don't know how the code is regenerated :( )

MFC uses a system of event message maps in the CView-derived class. For example in a newly created MFC SDI doc/view program, you will see this in the *.cpp file

IMPLEMENT_DYNCREATE(CMFCApplication1View, CView)

BEGIN_MESSAGE_MAP(CMFCApplication1View, CView)
    // Standard printing commands
    ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
END_MESSAGE_MAP()

When an event occurs, such as a keystroke, the MFC message pump will eventually search the message map to see if an event handler has been established to process that event. In the code above the only event handlers enabled are for three of the menu options. So when you click on the File Print menu item MFC will call the function named OnFilePrint().

Now if you use VC++ IDE and right click CView::OnFilePrint then select from the popup menu "Go To Definition", VC++ IDE will take you to the line where it's declared, in this case stdafx.h

    // not mapped commands - must be mapped in derived class
    afx_msg void OnFilePrint();
    afx_msg void OnFilePrintPreview();

what means 'afx_msg'?
isn't a C++ commands\code

afx_msg is just a user-defined data type. Normal c++ stuff.

right click afx_msg and the IDE will take you to where it's declared and defined. If you dont' know what something means then just right click on it and select "Go to definition".

MFC has a very very long learning curve, it takes about a year of constant studying MFC in order to learn it pretty well.

thanks maybe i can thot in anotherthing ;)
sorry, if i take time to answer i have problems with mails notifications, on posts, when i'm log in...strange but true

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.