1

In SystemC module constructor can be defined using SC_CTOR macro:

#define SC_MODULE(user_module_name)                   \
    struct user_module_name : ::sc_core::sc_module

#define SC_CTOR(user_module_name)                     \
    typedef user_module_name SC_CURRENT_USER_MODULE;  \
    user_module_name( ::sc_core::sc_module_name )

Need to understand use scope specifier operator before sc_core. As per the macro user can generate constructor but it takes argument sc_module_name.

User module struct user module is inheriting class sc_module, In class sc_module there are different overloaded constructor like

sc_module();
sc_module( const sc_module_name& nm ); /* for those used to old style */

/* DEPRECATED */ sc_module( const char* nm );
/* DEPRECATED */ sc_module( const std::string& nm );

As per SC_CTOR it is declaring any object of type sc_moduke_name in constructor body if expanded the constructor will take form user_module(::sc_core::sc_module) { /*constructor body*/}. To understand simulate same code but failed to create object of type struct B

class mname {
    private:
            char *name;
    public:
            mname() {
                    cout << "mname constructor invoked\n";
                    cout << "\t name\t" << name;
            }
};

class A {
    friend class mname;
    private:
    mname * mn;
    public:
    A() {
            cout << "A constructor called\n";
    }
    A(const mname& m) {
            cout << "freind class constructor of A\n";
    }
    A(const std:: string& s) {
            cout << "string type construfctor\n";
    }
};

struct B: A {
    B(mname ) {
            cout     << "struct b cosntructor called\n";
    }

};

int main() {

    B obj(mname);
    return 0;
}
anshkun
  • 105
  • 1
  • 12

1 Answers1

3

A typical code to create a module is :

SC_MODULE(my_module)
{
   SC_CTOR(my_module)
   {

   }
};

With macros, this code will expand to:

struct my_module : ::sc_core::sc_module
{
   typedef my_module SC_CURRENT_USER_MODULE;
   my_module( ::sc_core::sc_module_name )
   {

   }
};

If you want to create an instance of this module, you need to pass an instance of ::sc_core::sc_module_name in the constructor and not the class name as you did in your example code. You can also pass something to create implicitly an instance of ::sc_core::sc_module_name as a const char* (cf ::sc_core::sc_module_name constructors)

my_module a("name_of_my_module");

or

::sc_core::sc_module_name module_name("another_name");
my_module b(module_name);

In your example code, just replace the main with :

int main() {
    mname name; // instance of mname
    B obj(name);
    return 0;
}
Guillaume
  • 1,277
  • 8
  • 11
  • There is one more query as struct my_module has defined only one constructor which is taking sc_module_name instance then how const char* implicitly create instance of sc_module_name. As per my understanding for this there will be one more constructor of type my_module(const char*) . May be not good in C++ concepts, Thanks in advance to provide some more details – anshkun Sep 04 '17 at 16:34
  • I understand the point that when char* is passed then sc_module is friend of sc_module_name, sc_module_name already defined constructor with char* argument but another issue is if sc_module_name creates constructor with two arguments sc_module_name(const char*, int) and when try to create B object like b obj("another name", 4) then it fails – anshkun Sep 04 '17 at 17:07