Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: C++: Two functions with same name dilema  (Read 9669 times)

0 Members and 1 Guest are viewing this topic.

tom64

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: C++: Two functions with same name dilema
« Reply #30 on: April 09, 2013, 09:32:45 am »
Quote
You're using the wrong header, as stated before.
It should be: #include <cmath>, which puts everything into the std namespace.

OMG that was just an example!!!!!!!!!

I meant that I might be including lots of other peoples files which don't use namespaces...
Newbie to C++ and SFML

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10829
    • View Profile
    • development blog
    • Email
Re: C++: Two functions with same name dilema
« Reply #31 on: April 09, 2013, 09:36:28 am »
OMG that was just an example!!!!!!!!!
OKAY!!!!! ;D

I meant that I might be including lots of other peoples files which don't use namespaces...
Yes, that's possible, that's also why namespaces were invented in the first place. I usually tend to not use libraries that don't make use of namespaces, unless there's no alternative and the names are uniquely enough (i.e. many C libraries use some ugly prefixes, but at least they make the names unique). ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

tom64

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: C++: Two functions with same name dilema
« Reply #32 on: April 09, 2013, 09:42:21 am »
But namespaces don't completely solve the problem. Because if you have tons of lazy programmers all giving you their lib, and they make namespaces like a:: or xyz:: then you're not only going to get function name conflicts but also namespace conflicts...

Anyway thanks everyone for your help. I feel smarter  :-*
Newbie to C++ and SFML

tom64

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: C++: Two functions with same name dilema
« Reply #33 on: April 09, 2013, 10:21:45 am »
SUCESS!

I actually managed to do it, and it works!?? Pretend that lib1 is MY library, and lib2 is the <math> library.

main.cpp
#include <iostream>

namespace myNamespace2{
        #include "lib2.h"
}

#include "lib1.h"

int main(){
        std::cout << func() << std::endl;
        return 0;
}

lib1.h
#pragma once

int func(){
        return myNamespace2::func();
}

lib2.h
#pragma once

int funcc(){
        return 3;
}

int func(){
        return funcc();
}

Outputs: 3

You all told me it couldn't be done!
« Last Edit: April 09, 2013, 10:26:39 am by tom64 »
Newbie to C++ and SFML

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: C++: Two functions with same name dilema
« Reply #34 on: April 09, 2013, 10:35:29 am »
Quote
You all told me it couldn't be done!
I said it would work only if the other library is completely inlined (everything in header, no compiled library). Which is the case here. But is it also the case in your real program?
Laurent Gomila - SFML developer

tom64

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: C++: Two functions with same name dilema
« Reply #35 on: April 09, 2013, 11:00:47 am »
Quote
You all told me it couldn't be done!
I said it would work only if the other library is completely inlined (everything in header, no compiled library). Which is the case here. But is it also the case in your real program?

Ah, nevermind, it doesn't work on math.h. Says 'floor is not a member of myNamespace2'

Also, I just realised now that round() isn't even inside <math>  ;D

I definitely need some sleep.

Thanks again everyone..

PS: Does anyone know if it's possible to only load/include a specific function from a file? e.g. Say I only wanted to include the floor() function from <cmath> without loading all the other functions, is that possible?
« Last Edit: April 09, 2013, 11:10:25 am by tom64 »
Newbie to C++ and SFML

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: C++: Two functions with same name dilema
« Reply #36 on: April 09, 2013, 11:56:32 am »
Quote
Does anyone know if it's possible to only load/include a specific function from a file? e.g. Say I only wanted to include the floor() function from <cmath> without loading all the other functions, is that possible?
No.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: C++: Two functions with same name dilema
« Reply #37 on: April 09, 2013, 12:34:25 pm »
Three pages discussion about how to workaround namespaces? tom64, you should really use namespaces the way they are intended to be used. As Laurent already mentioned in the beginning, your code will eventually get more readable if you see the namespaces for each function and class.

Introducing dirty hacks because you don't want to write a few more characters is a very bad idea. Especially since the pure writing usually isn't the limiting factor at programming, and today's IDEs and tools are capable to auto-complete.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

tom64

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: C++: Two functions with same name dilema
« Reply #38 on: April 09, 2013, 12:46:07 pm »
Quote
Introducing dirty hacks because you don't want to write a few more characters is a very bad idea.

I know, but I'm talking about doing it for very generic/general/basic functions, not everything.
Newbie to C++ and SFML

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: C++: Two functions with same name dilema
« Reply #39 on: April 09, 2013, 12:58:29 pm »
Quote
I know, but I'm talking about doing it for very generic/general/basic functions, not everything.
It's still bad. Even if it was for a single function.
Laurent Gomila - SFML developer