?
Solved

How do I pass argv to a class

Posted on 2001-06-20
4
Medium Priority
?
487 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
mnashadka earned 200 total points
ID: 6209896
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
ID: 6209938
Thanks mnashadka,

Good answer.  Please have the points.

Keep up the good work.
0
 
LVL 22

Expert Comment

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

Author Comment

by:carlpaddick
ID: 6223717
nietod,

Again, I posted this comment as an accepted answer.

Thanks,

Carl
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.
Suggested Courses

762 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