How can I realize this? Maybe beyond C++ capability?

Now I defined several boundary conditions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class wall_t {
...
};

class inlet_t {
...
};

class outlet_t {
...
};

class couple_t {
...
};


and I also defined

1
2
3
class block_t {
...
};


now I have a block, and I want this block to be one of those four boundary conditions, and I will tell which one it is when the programming is running(after compilation)

I will write this in my program,

1
2
cin>>boundary;
block_t block1(boundary);


if the boundary is wall, then I can get the variables and functions defined in wall_t, and the other boundary types' variables and functions are not exist at all.

How can I do this? How to define block_t and related using functions? Thanks.
Last edited on
Make block_t the parent class, and then have the other four inherit from it.

1
2
3
4
5
class block_t {/**/};

class wall_t : public block_t {/**/};

//etc 


Tada, polymorphism.

*EDIT* You'll probably have to rework some of your design decisions. Passing boundary (whatever that is) to a ctor to determine which type is extremely sketch. I can help you figure things out further in the morning. zzzzzzZZZzzz
Last edited on
Looks very promising, thanks alot! xismn, waiting for your details. LMAO!
Another option is to use typeid to work out what kind of class you have recieved. (http://www.cplusplus.com/reference/typeinfo/type_info). Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <typeinfo>

class block_t {
    public:
        block_t(void* object);
        // ...

    private:
        void* _object;
        enum { NOTYPE, WALL, INLET, OUTLET, COUPLE } _type;
};

block_t::block_t(void* object) : _object(object) {
    if (object == nullptr)
        _type = NOTYPE;
    else if (typeid(*object) == typeid(wall_t))
        _type = WALL;
    else if (typeid(*object) == typeid(inlet_t))
        _type = INLET;
    else if (typeid(*object) == typeid(outlet_t))
        _type = OUTLET;
    else if (typeid(*object) == typeid(couple_t))
        _type = COUPLE;
    else
        _type = NOTYPE;
}

// to use it:
switch (_type) {
    case WALL: {
        wall_t* obj = static_cast<wall_t*>(_object);
        obj->doSomething();
        // etc
    } break;

    // etc
}

// to set it...
block_t block1 (boundary); // Sets the type to whatever boundary is 



I'm just posting this for completeness, because I have not seen this on the forum's before and I reckon it should be on here. However, this is complicated and slightly inefficient to implement, so you should definitely use polymorphism like @xismn said, unless there is a specific reason not to use, or this method would be easier to use.
Ugh, I'm sorry. The only example I could come up with was pretty much identical to NT3's.
The thing is, you will have to do either one of these things:

1.) Design your classes in such a way that member variables are the same and not unique to each class.

2.) Find a way to make pointer casting pretty.

This is typically where design patterns come in handy. In your case, I would look into the factory design pattern.
Now I have defined a class called PDEBoundary_t and three derived class based on that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class PDEBoundary_t {
public:
	virtual void setValue() {}
	virtual void setDerivative() {}
	virtual numeric_t value() {}
};

class Dirichlet_t:public PDEBoundary_t {
	numeric_t val;
public:
	Dirichlet_t() {val=0;}
	Dirichlet_t(numeric_t v) {val=v;}
	inline void setValue(numeric_t v) {val=v;}
	inline numeric_t value() {return val;}
};

class Neumann_t:public PDEBoundary_t {
	numeric_t der;
public:
	Neumann_t() {der=0;}
	Neumann_t(numeric_t v) {der=v;}
	inline void setDerivative(numeric_t v) {der=v;}
	inline numeric_t value(numeric_t delta, numeric_t theOther, bool first) {return first?der*delta+theOther:theOther-der*delta;}
};

class Cauchy_t:public PDEBoundary_t {
	numeric_t val, der;
public:
	Cauchy_t() {
		val=0;
		der=0;
	}
	Cauchy_t(numeric_t v, numeric_t d) {
		val=v;
		der=d;
	}
	inline void setValue(numeric_t v) {val=v;}
	inline void setDerivative(numeric_t v) {der=v;}
	inline numeric_t value(numeric_t delta, bool first) {return first?der*delta+val:val-der*delta;}
};


Now I have a program, I want to read in a random block, and I want to assign one of the three boundary condition to this block, and if Neumann_t is assigned, program will ask about the derivative value, how can I do that?

Sorry, I still confused!
Topic archived. No new replies allowed.