Solved

How do I pass argv to a class

Posted on 2001-06-20
4
478 Views
Last Modified: 2010-04-02
I want to be able to pass the argv parameter given within the main() function of an app to a class, and assign it to a class member, such as :-

// from main

main(int argc, char* argv[])
{
   CMyClass myClass;

   myClass.Init(argc, argv);

......

}

// in myClass

CMyClass::Init(int argc, char* argv[])
{
   m_nArgc = argc;

  m_szArgv = argv;     // is this correct;

}

What I would like to know, is what data type must m_szArgv be declared as and how should argv be assigned to it?
0
Comment
Question by:carlpaddick
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
mnashadka earned 50 total points
Comment Utility
m_szArgv should be a char **, and the way you assign it is fine.  If you don't want to mess with char **'s, you might want to look at assigning argv to a vector of strings, like:

class CMyClass
{
...
  std::vector<std::string> m_Argv; // Could also be CString
};

// in myClass

CMyClass::Init(int argc, char* argv[])
{
  for(int i = 0; i < argc; ++i)
    m_Argv.push_back(argv[i]);
}
And you can still use the subscript operator to get data out later.  e.g. std::string param1 = m_Argv[0];

I hope this helps you.
0
 

Author Comment

by:carlpaddick
Comment Utility
Thanks mnashadka,

Good answer.  Please have the points.

Keep up the good work.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Does anyone know if this accepted answer was posted as a comment or as an answer?
0
 

Author Comment

by:carlpaddick
Comment Utility
nietod,

Again, I posted this comment as an accepted answer.

Thanks,

Carl
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now