how can i test if a function is defined?

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

46 Replies - 2290 Views - Last Post: 11 May 2018 - 07:38 AM Rate Topic: ***-- 2 Votes

#16 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 755
  • Joined: 04-March 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 07:21 AM

in these case, the macro is for avoid me repeat some code. but i get your point
Was This Post Helpful? 0
  • +
  • -

#17 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6502
  • View blog
  • Posts: 22,261
  • Joined: 05-May 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 07:36 AM

Virtual functions are meant to facilitate polymorphism. If these functions you are creating are only for the use of the class in which they are declared (e.g. they will always be private) why would you even need prototypes? Just define them in the header file as private class member functions, or have them as static functions in the .cpp file before the class member function definitions.

Are you working on some kind of 4GL to 3GL code generator but trying to do it on the cheap by using macros? Or are you trying to roll your own Windows MFC?

In general, GUIs and game engines in C and C++ implement events where the possible types of events are unknown or unbounded by using messages instead of actual functions or callbacks that map to exactly on function per event type.
Was This Post Helpful? 1
  • +
  • -

#18 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 755
  • Joined: 04-March 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 07:36 AM

- the base class have the virtual functions;
- the derived class can re-defined the virtual base functions.... but i must prototype them for i change them on Global Scope:
class base
{

public:
    virtual void MouseClick(){;};
	test()
	{
		MouseClick(){;};
	}

	void call()
	{
		MouseClick(){;};
	}
};

class InstanceName : public base
{ 
public:
	InstanceName(){;}

	~InstanceName(){;}

public:
	void MouseClick();// i must protype it for i define it on Global Scope
//	void Move();
        //the macro is for help me re-prototype the virtual functions
        //i want use a global macro and define what i need              
} InstanceName; //yes these class is for be used like a variable and not instances 

//i belive that we call the Global Scope(these section)
void InstanceName::MouseClick() //InstanceName it's a class name, that's why i can use it on Global Scope
{
    std::cout << "Mouse click";
}


Was This Post Helpful? 0
  • +
  • -

#19 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6502
  • View blog
  • Posts: 22,261
  • Joined: 05-May 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 08:39 AM

That is not "global scope".

If you want the compiler to help you in identifying if you are correctly overriding an existing virtual function, use the override keyword.

How is Move() related to MouseClick()?

C++ is naturally verbose. It's the nature of the language design based on its origins as being implemented originally as a pre-compiler to C. If you are looking to save on typing and be less verbose, take a look at:
vrsperanza/CPY
Was This Post Helpful? 1
  • +
  • -

#20 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 755
  • Joined: 04-March 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 08:52 AM

"That is not "global scope"."
please correct me. i thot it was.


i can't define the MouseClick, if InstanceName don't have it's prototype...
void InstanceName::MouseClick() override
{
    std::cout << "Mouse click";
}

right?
error message:
"virt-specifiers in 'MouseClick' not allowed outside a class definition|"

"How is Move() related to MouseClick()?"
it's a comment.. forget that ;)

This post has been edited by Cambalinho: 06 May 2018 - 08:55 AM

Was This Post Helpful? 0
  • +
  • -

#21 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6502
  • View blog
  • Posts: 22,261
  • Joined: 05-May 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 09:42 AM

That is not a "global scope". That is a function override.

In older versions of C++, there was not an override keyword and so people would run into various problems when they would accidentally override a base virtual function, or not realize that they are not overriding the virtual function.

You use the override keyword in the declaration as the error message states, and shown in the documentation.
Was This Post Helpful? 1
  • +
  • -

#22 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 755
  • Joined: 04-March 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 10:11 AM

what is a problem with C++ that, so many power, and don't let me control everything? :(
(i'm sorry, but i'm tiered)
Was This Post Helpful? 0
  • +
  • -

#23 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6502
  • View blog
  • Posts: 22,261
  • Joined: 05-May 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 12:27 PM

The power comes from using the language the way it was meant to be used. It looks like you are looking for a different language.
Was This Post Helpful? 1
  • +
  • -

#24 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 755
  • Joined: 04-March 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 12:33 PM

to be honest i'm not locking for another language, but 1 specific method for test if the function is on class.
that template class do the work. but if i have a function prototype, without definition, i get '1'. so, maybe, that template class can be updated for resolve my problem...(i think it's the best shot.... but i understand you(both) said.
from C++ several power, i don't belive that i can't control what i need
Was This Post Helpful? 0
  • +
  • -

#25 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 755
  • Joined: 04-March 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 12:55 PM

finally i get something:

You can do this in GCC using its weak function attribute extension:

void func() __attribute__((weak)); // weak declaration must always be present

int main() {
  if (func) func();
  // ...
}

// optional definition:
void func() { ... }

This works even if func() is defined in another .c file or a library.

i read these on these forum(google search): https://stackoverflo...ck-if-it-exists

but if the function isn't defined, i will get an error(see the picture), so i did use the 'try':
template <typename CBase>
class test
{
    CBase* atPointer = nullptr;
public:
    void MouseClick(){;};
    test(CBase *clsPointer) : atPointer(clsPointer)
    {
        if(TypeHasToString<CBase>::value)
        {
            try
            {
                clsPointer->MouseClick();
            }
            catch(...)
            {
                MouseClick();
            }
        }


    }
///...


https://imgur.com/a/0e9f8ZX

(these error only happens when the function isn't defined)

This post has been edited by Cambalinho: 06 May 2018 - 12:55 PM

Was This Post Helpful? 0
  • +
  • -

#26 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6502
  • View blog
  • Posts: 22,261
  • Joined: 05-May 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 01:26 PM

To quote from the Wikipedia article regarding weak symbols:

Quote

Weak symbols are not mentioned by the C or C++ language standards; as such, inserting them into code is not very portable. Even if two platforms support the same or similar syntax for marking symbols as weak, the semantics may differ in subtle points, e.g. whether weak symbols during dynamic linking at runtime lose their semantics or not.

Was This Post Helpful? 1
  • +
  • -

#27 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 755
  • Joined: 04-March 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 01:36 PM

that i didn't knew, but think with me: these error only happens if the the function isn't defined(i had tested)... so why the 'try' don't catch the error? it's similar to a memory leak(the window style)
Was This Post Helpful? 0
  • +
  • -

#28 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6502
  • View blog
  • Posts: 22,261
  • Joined: 05-May 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 02:00 PM

You'll need to provide a more complete minimal example to show what you are talking about, but if I had to guess the C++ catch can only catch C++ exceptions. It can't handle system level errors when you try to jump to code (e.g. call a function) that doesn't exist.
Was This Post Helpful? 1
  • +
  • -

#29 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 755
  • Joined: 04-March 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 02:09 PM

heres the entire code:
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <functional>
#include <typeinfo>
#include <exception>

template <typename CBase>
class test
{
    CBase* atPointer = nullptr;
public:
    void MouseClick(){;};
    test(CBase *clsPointer) : atPointer(clsPointer)
    {

        try
        {
            clsPointer->MouseClick();
        }
        catch(std::exception& e)
        {
            MouseClick();
        }
    }
    void call()
    {
        atPointer->MouseClick();
    }
};

class InstanceName : public test<InstanceName>
{ // manual expansion of ClassWithEvents
public:
    InstanceName() : test(this){;}
    ~InstanceName(){;}
public:
    void MouseClick() __attribute__((weak));
    void Move() __attribute__((weak));
} InstanceName;
               
/*void InstanceName::MouseClick()
{
    std::cout << "Mouse click";
}*/


/*void b::Move()
{
   std::cout << "Move";
}*/

int main()
{
    std::cin.get();
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#30 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6502
  • View blog
  • Posts: 22,261
  • Joined: 05-May 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 02:15 PM

Notice here how the division by zero exception is not caught:
Posted Image

Spoiler

Was This Post Helpful? 1
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4