Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 months ago.
Improve this question
The project calls functions from a library. I want to move those functions behind a namespace so that is easier to spot the places on the codebase where those functions are being called.
How functions are being called:
#include "foo.h"
int main()
{
foo();
bar();
return 0;
}
How I want to call them:
#include "myfoo.h"
int main()
{
thatlibrary::my_foo();
thatlibrary::my_bar();
return 0;
}
How I implemented that:
myfoo.h
namespace thatlibrary
{
void my_foo();
void my_bar();
}
myfoo.cpp
namespace thatlibrary
{
void my_foo()
{
foo();
}
void my_bar()
{
bar();
}
}
Wondering if there is any other solution? Perhaps more elegant.
Just use a using declaration in your namespace:
// global namespace
int foo() {
return 1;
}
namespace thatlibrary {
using ::foo;
}
auto i = thatlibrary::foo();
Notice that the name foo is still available in the global namespace, though.
// in global namespace
auto j = foo(); // works just fine
// from anywhere
auto k = ::foo(); // works just fine
I think the library is poorly designed, if it declares all its stuff in the global namespace.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
If I have the following code :
void foo1()
{
NS1::Type1 instance1;
NS1::Type2 instance2;
NS1::Type3 instance3;
}
void foo2()
{
NS2::Type1 instance1;
NS2::Type2 instance2;
NS2::Type3 instance3;
}
How to factorize this function ?
I can call foo1 from NS1, and foo2 from NS2.
How to “pass” an namespace as argument?
There is no way to do that.
It is possible to write a reusable template for your foos if you use classes instead of namespaces:
struct NS1 {
using Type1 = int;
using Type2 = float;
using Type3 = std::string;
};
struct NS2 {
using Type1 = long;
using Type2 = double;
using Type3 = std::string;
};
template<class T>
void foo() {
typename T::Type1 instance1;
typename T::Type2 instance2;
typename T::Type3 instance3;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
given the next code:
#include <iostream>
using std::cout;
using std::endl;
using std::cerr;
class A {
public:
virtual ~A() {
}
};
class B: public A {
public:
};
int main() {
int n = 4;
A a;
A& base = a;
B* ptr = dynamic_cast<B*>(&base);
if (ptr == NULL) {
cerr << "base is not a B";
}
try {
B& derived = dynamic_cast<B&>(base);
derived = *ptr;
} catch (std::bad_cast&) { // ERROR
cerr << "base is not a B";
}
if (n == 3) {
}
return 0;
}
I get this message error and I don't understand what is the reason and how can I fix it?
'bad_cast' in namespace 'std' does not name a type
If you look up the documentation at http://en.cppreference.com/w/cpp/types/bad_cast it tells you at the top which include is required for each class/function. In this case you need to include <typeinfo>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is there a way to allocate a variable within the scope of a parameter list? By using new we can do the following :
Class A{ /*... snip ...*/ };
void myFunc(A* a){ }
int main(...){
myFunc(new A());
return 0;
}
This will create a new A. What if the signature of myFunc was
void myFunc(A a);
instead. Is there a syntax to create local instance inside the myFunc() parameter list? I'm looking for something like
myFunc(A());
or
myFunc(A a());
Another use would be for something like :
A a(123);
if(a == A(123)){ }
The net effect is to save one line, but it also creates a scope within the parameters list which makes me wonder if it is allowed at all.
If you just want to create a variable to pass to the function you can use a aggregate initialization / list initialization
#include <iostream>
#include <cmath>
using namespace std;
class A{ /*... snip ...*/ };
void myFunc(A a){ }
int main(){
myFunc(A{});
return 0;
}
Live Example
You can also use this with classes that have constructors that take multiple parameters
#include <iostream>
#include <cmath>
using namespace std;
class A
{
private:
int foo;
int bar;
double foobar;
public:
A(int a, int b, double c) : foo(a), bar(b), foobar(c) {}
};
void myFunc(A a){ }
int main(){
myFunc(A{1,2,3.0});
return 0;
}
Live Example
C++ supports this with the myFunc(A()); syntax you posed in your question.
#include <stdio.h>
char lazybuff[500];
class Point
{
public:
Point (double x, double y) : m_x(x), m_y(y) { }
char * ToString (void) { sprintf (lazybuff, "%f, %f", m_x, m_y); return lazybuff; }
private:
double m_x, m_y;
};
void print_point (Point print_me)
{
printf ("%s\n", print_me.ToString());
}
int main (void)
{
print_point (Point(5, 3));
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I get an input variable in main function. and I want to use this variable in the external function2 that it already is called as a external function again by function1(like the code below). but this variable is undefined at function2. please help me. thanks for all answers. this is an overview of my code:
int main ()
{
int boogh;
cin >> boogh;
function1 (x,y);
}
function1(int x,int y)
{.
.
function2(w,z);
.
.
}
function2(int w, int z)
{
if (boogh>5)
{.
do some thing
.
.
}
}
the function1 and function2 are recursive
The variable is scoped within main, so you can't access it there. You need to get it (or a copy of it) into the scope of function2. One way is to pass it as a function parameter - to both functions, since it has to go through function1 to reach function2:
void function1(int x, int y, int boogh) {
//...
function2(w, z, boogh);
//...
}
void function2(int w, int z, int boogh) {
if (boogh > 5) { // the value is available here
//...
}
}
int main() {
int boogh;
cin >> boogh;
function1(x,y,boogh);
}
Or you could encapsulate the variable and the function(s) that use it in a class:
struct thingy {
int boogh;
void function1(int x,int y) {
//...
function2(w, z);
//...
}
void function2(int w,int z) {
if (boogh > 5) { // class member accessible here
//...
}
}
};
int main() {
thingy t;
cin >> t.boogh;
t.function1(x,y);
}
Or you could use a global variable, but that's nearly always a bad idea.
You must pass the required variable to the functions either by value or by reference:
Passing by value for your case:
// Example program
#include <iostream>
#include <string>
using namespace std;
void function1(int x);
void function2(int x);
int main ()
{
int boogh;
cin>>boogh;
function1 (boogh);
}
void function1(int x)
{
function2(x);
}
void function2(int y)
{
int boogh=y;
if (boogh>5)
{
//do something here
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am learning about Delegating Constructors.
#include <iostream>
using namespace std;
class A{
public:
A(int i, int j): num1(i), num2(j){
average=(num1+num2)/2;
}
A(): A(0){ }
A(int i): A(i, 0){ }
private:
int num1;
int num2;
int average;
};
and this is what I succeed to understend. I don't know such it works in a int main().
I think you need just an example to how to create objects from A:
int main()
{
A obj1(10, 20); // Calls A(10, 20) average: 15
A obj2; // Calls A() -> A(0) -> A(0, 0) average: 0
A obj3(100); // Calls A(100) -> A(100, 0) average: 50
}