原文http://blog.csdn.net/ztz0223/article/details/7596025
前一讲:
已经说过了,如何生成一个结构体,添加成员变量,添加带参数的成员函数,那么如何看到效果呢?
这就是本文要说的了:变换->UML to C++
回忆一下,之前建立的模型--c++类图。
现在要用这个类图类生成相应的代码:
步骤一,选择建模文件example_uml,右键,弹出窗口选择变换-》新建配置:
步骤二,如图输入配置名:
步骤三,创建目标容器:
步骤四,创建生成的c++工程,输入example_cpp作为c++的工程名:
步骤五,点击完成,转换配置自然就完成了,最后结果如图所示了:
可以点击窗口中 UML to C++属性,定制需要的一些操作。
步骤六,点击运行便可生成C++代码了:
最后生成的代码工程如下:
我们再看看代码,和上一讲所说如何?
ok,双击 my_struct.h :
[cpp] view plaincopy
- #ifndef MY_STRUCT_H
- #define MY_STRUCT_H
- //Begin section for file my_struct.h
- //TODO: Add definitions that you want preserved
- //End section for file my_struct.h
- //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
- struct my_struct
- {
- //Begin section for my_struct
- //TODO: Add attributes that you want preserved
- //End section for my_struct
- private:
- //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
- double m_dbl;
- public:
- //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
- int * m_func(long arg_in);
- }; //end struct my_struct
- #endif
my_struct.cpp:
[cpp] view plaincopy
- #include "my_struct.h"
- //Begin section for file my_struct.cpp
- //TODO: Add definitions that you want preserved
- //End section for file my_struct.cpp
- //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
- int * my_struct::m_func(long arg_in)
- {
- //TODO Auto-generated method stub
- return 0;
- }
明显可见了,上一文章说的,新建的类成员变量和类的成员函数都和建模一致了。
如果我们修改了之前的类图设计,代码也可以跟着一起变化的,操作如下咯:
1、我在之前的结构体里面添加一个静态函数,static int do_nothing();
2、保存整个工程,在工作区选择变换配置文件tc1.tc标签栏,点击运行
3、会提示你,新的修改要写入之前的文件,确定就是了:
4、我们再看看代码,头文件 my_struct.h:
[cpp] view plaincopy
- #ifndef MY_STRUCT_H
- #define MY_STRUCT_H
- //Begin section for file my_struct.h
- //TODO: Add definitions that you want preserved
- //End section for file my_struct.h
- //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
- struct my_struct
- {
- //Begin section for my_struct
- //TODO: Add attributes that you want preserved
- //End section for my_struct
- private:
- //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
- double m_dbl;
- public:
- //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
- int * m_func(long arg_in);
- //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
- static int do_nothing();
- }; //end struct my_struct
5、看看cpp实现文件:
[cpp] view plaincopy
- #include "my_struct.h"
- //Begin section for file my_struct.cpp
- //TODO: Add definitions that you want preserved
- //End section for file my_struct.cpp
- //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
- int * my_struct::m_func(long arg_in)
- {
- //TODO Auto-generated method stub
- return 0;
- }
- //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
- int my_struct::do_nothing()
- {
- //TODO Auto-generated method stub
- return 0;
- }
一切都是如此美好,哈哈。